1111 Online Map (30 分)——甲级(2次Dijkstra+记录路径+vector)

14 篇文章 0 订阅
9 篇文章 0 订阅

Input our current position and a destination, an online map can recommend several paths. Now your job is to recommend two paths to your user: one is the shortest, and the other is the fastest. It is guaranteed that a path exists for any request.

Input Specification:
Each input file contains one test case. For each case, the first line gives two positive integers N (2≤N≤500), and M, being the total number of streets intersections on a map, and the number of streets, respectively. Then M lines follow, each describes a street in the format:

V1 V2 one-way length time
where V1 and V2 are the indices (from 0 to N−1) of the two ends of the street; one-way is 1 if the street is one-way from V1 to V2, or 0 if not; length is the length of the street; and time is the time taken to pass the street.

Finally a pair of source and destination is given.

Output Specification:
For each case, first print the shortest path from the source to the destination with distance D in the format:

Distance = D: source -> v1 -> … -> destination
Then in the next line print the fastest path with total time T:

Time = T: source -> w1 -> … -> destination
In case the shortest path is not unique, output the fastest one among the shortest paths, which is guaranteed to be unique. In case the fastest path is not unique, output the one that passes through the fewest intersections, which is guaranteed to be unique.

In case the shortest and the fastest paths are identical, print them in one line in the format:

Distance = D; Time = T: source -> u1 -> … -> destination
Sample Input 1:
10 15
0 1 0 1 1
8 0 0 1 1
4 8 1 1 1
3 4 0 3 2
3 9 1 4 1
0 6 0 1 1
7 5 1 2 1
8 5 1 2 1
2 3 0 2 2
2 1 1 1 1
1 3 0 3 1
1 4 0 1 1
9 7 1 3 1
5 1 0 5 2
6 5 1 1 2
3 5
Sample Output 1:
Distance = 6: 3 -> 4 -> 8 -> 5
Time = 3: 3 -> 1 -> 5
Sample Input 2:
7 9
0 4 1 1 1
1 6 1 1 3
2 6 1 1 1
2 5 1 2 2
3 0 0 1 1
3 1 1 1 3
3 2 1 1 2
4 5 0 2 2
6 5 1 1 2
3 5
Sample Output 2:
Distance = 3; Time = 4: 3 -> 2 -> 5

题目大意:求两条最路:最短路和最快路。如果最短路不唯一,输出最快的一条;如果最快路不唯一,输出步数最少的一条。如果所求的两条路相同,输出在同一行;否则分开输出。

思路:两次Dijkstra就可以了。

代码如下:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#include <map>
#define maxn 505
#define inf 0x3f3f3f3f
using namespace std;
int e[maxn][maxn][2], vis[maxn];
int dis[maxn], dt[maxn], t[maxn], step[maxn];
int n, m, st, en;
void show(vector<int>pre, int i)
{
    if(i == st)
    {
        printf("%d", st);
        return ;
    }
    show(pre, pre[i]);
    printf(" -> %d", i);
}
int main()
{
    cin >> n >> m;
    int i, j;
    vector<int>pre1(n, 0), pre2(n, 0);
    for(i=0; i<n; i++)
    {
        e[i][i][0] = e[i][i][1] = 0;
        vis[i] = 0;
        for(j=i+1; j<n; j++)
        {
            e[i][j][0] = e[j][i][0] = inf;
            e[i][j][1] = e[j][i][1] = inf;
        }
    }
    while(m--)
    {
        int a, b, tag, c, d;
        cin >> a >> b >> tag >> c >> d;
        e[a][b][0] = c;
        e[a][b][1] = d;
        if(!tag)
        {
            e[b][a][0] = c;
            e[b][a][1] = d;
        }
    }
    cin >> st >> en;
    /* 求最短路 */
    for(i=0; i<n; i++) dis[i] = i == st ? 0 : inf;
    dt[st] = 0;
    pre1[st] = -1;
    for(i=0; i<n; i++)
    {
        int k = -1;
        int min = inf;
        for(j=0; j<n; j++)
            if(!vis[j] && min > dis[j])
            {
                min = dis[j];
                k = j;
            }
        if(k == -1) break;
        vis[k] = 1;
        for(j=0; j<n; j++)
        {
            if(!vis[j])
            {
                if(dis[j] > dis[k] + e[k][j][0])
                {
                    dis[j] = dis[k] + e[k][j][0];
                    dt[j] = dt[k] + e[k][j][1];
                    pre1[j] = k;
                }
                else if(dis[j] == dis[k] + e[k][j][0] && dt[j] > dt[k] + e[k][j][1])
                {
                    dt[j] = dt[k] + e[k][j][1];
                    pre1[j] = k;
                }
            }
        }
    }
    /* 求最快路 */
    memset(vis, 0, sizeof(vis));
    for(i=0; i<n; i++) t[i] = i == st ? 0 : inf;
    step[st] = 0;
    pre2[st] = -1;
    for(i=0; i<n; i++)
    {
        int k = -1;
        int min = inf;
        for(j=0; j<n; j++)
            if(!vis[j] && min > t[j])
            {
                min = t[j];
                k = j;
            }
        if(k == -1) break;
        vis[k] = 1;
        for(j=0; j<n; j++)
        {
            if(!vis[j])
            {
                if(t[j] > t[k] + e[k][j][1])
                {
                    t[j] = t[k] + e[k][j][1];
                    step[j] = step[k] + 1;
                    pre2[j] = k;
                }
                else if(t[j] == t[k] + e[k][j][1] && step[j] > step[k] + 1)
                {
                    step[j] = step[k] + 1;
                    pre2[j] = k;
                }
            }
        }
    }
    if(pre1 == pre2)/* vector可以直接判断是否相等 */
    {
        printf("Distance = %d; Time = %d: ", dis[en], t[en]);
        show(pre1, en);
    }
    else
    {
        printf("Distance = %d: ", dis[en]);
        show(pre1, en);
        printf("\n");
        printf("Time = %d: ", t[en]);
        show(pre2, en);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值