本题要求你实现一个天梯赛专属在线地图,队员输入自己学校所在地和赛场地点后,该地图应该推荐两条路线:一条是最快到达路线;一条是最短距离的路线。题目保证对任意的查询请求,地图上都至少存在一条可达路线。

输入格式:

输入在第一行给出两个正整数N(2 <= N <=500)和M,分别为地图中所有标记地点的个数和连接地点的道路条数。随后M行,每行按如下格式给出一条道路的信息:

V1 V2 one-way length time

其中V1和V2是道路的两个端点的编号(从0到N-1);如果该道路是从V1到V2的单行线,则one-way为1,否则为0;length是道路的长度;time是通过该路所需要的时间。最后给出一对起点和终点的编号。

输出格式:

首先按下列格式输出最快到达的时间T和用节点编号表示的路线:

Time = T: 起点 => 节点~1~ => ... => 终点

然后在下一行按下列格式输出最短距离D和用节点编号表示的路线:

Distance = D: 起点 => 节点~1~ => ... => 终点

如果最快到达路线不唯一,则输出几条最快路线中最短的那条,题目保证这条路线是唯一的。而如果最短距离的路线不唯一,则输出途径节点数最少的那条,题目保证这条路线是唯一的。

如果这两条路线是完全一样的,则按下列格式输出:

Time = T; Distance = D: 起点 => 节点~1~ => ... => 终点

输入样例1:

10 15
0 1 0 1 1
8 0 0 1 1
4 8 1 1 1
5 4 0 2 3
5 9 1 1 4
0 6 0 1 1
7 3 1 1 2
8 3 1 1 2
2 5 0 2 2
2 1 1 1 1
1 5 0 1 3
1 4 0 1 1
9 7 1 1 3
3 1 0 2 5
6 3 1 2 1
5 3

输出样例1:

Time = 6: 5 => 4 => 8 => 3
Distance = 3: 5 => 1 => 3

输入样例2:

7 9
0 4 1 1 1
1 6 1 3 1
2 6 1 1 1
2 5 1 2 2
3 0 0 1 1
3 1 1 3 1
3 2 1 2 1
4 5 0 2 2
6 5 1 2 1
3 5

输出样例2:

Time = 3; Distance = 4: 3 => 2 => 5
跟甲级题Online Map基本是同一道题,要求稍有不同。
代码:
#include <stdio.h>
#include <string.h>
#define inf 0x3f3f3f3f
int m,n,source,destination,a,b,w,l,t;
int length[501][501],times[501][501],dis[501],cost[501],dis1[501],num[501],vis[501],path1[501],path2[501];
void getpath1(int x) {
    if(x != source) {
        getpath1(path1[x]);
        printf(" => ");
    }
    printf("%d",x);
}
void getpath2(int x) {
    if(x != source) {
        getpath2(path2[x]);
        printf(" => ");
    }
    printf("%d",x);
}
int equals(int x) {
    if(path1[x] != path2[x])return 0;
    else if(x == source)return 1;
    return equals(path1[x]);
}
int main() {
    scanf("%d%d",&n,&m);
    for(int i = 0;i < n;i ++) {
        for(int j = 0;j < n;j ++) {
            length[i][j] = times[i][j] = inf;
        }
        dis[i] = cost[i] = dis1[i] = inf;
        path1[i] = path2[i] = -1;
    }
    for(int i = 0;i < m;i ++) {
        scanf("%d%d%d%d%d",&a,&b,&w,&l,&t);
        if(w) {
            length[a][b] = l;
            times[a][b] = t;
        }
        else {
            length[a][b] = length[b][a] = l;
            times[a][b] = times[b][a] = t;
        }
    }
    scanf("%d%d",&source,&destination);
    dis[source] = cost[source] = dis1[source] = 0;
    while(1) {
        int t = -1,mi = inf;
        for(int i = 0;i < n;i ++) {
            if(!vis[i] && mi > cost[i]) {
                mi = cost[i];
                t = i;
            }
        }
        if(t == -1)break;
        vis[t] = 1;
        for(int i = 0;i < n;i ++) {
            if(vis[i] || times[t][i] == inf)continue;
            if(cost[i] > cost[t] + times[t][i]) {
                path2[i] = t;
                cost[i] = cost[t] + times[t][i];
                dis1[i] = dis1[t] + length[t][i];

            }
            else if(cost[i] == cost[t] + times[t][i] && dis1[i] > dis1[t] + length[t][i]) {
                dis1[i] = dis1[t] + length[t][i];
                path2[i] = t;
            }
        }
    }
    memset(vis,0,sizeof(vis));
    while(1) {
        int t = -1,mi = inf;
        for(int i = 0;i < n;i ++) {
            if(!vis[i] && mi > dis[i]) {
                mi = dis[i];
                t = i;
            }
        }
        if(t == -1)break;
        vis[t] = 1;
        for(int i = 0;i < n;i ++) {
            if(vis[i] || length[t][i] == inf)continue;
            if(dis[i] > dis[t] + length[t][i]) {
                path1[i] = t;
                dis[i] = dis[t] + length[t][i];
                num[i] = num[t] + 1;
            }
            else if(dis[i] == dis[t] + length[t][i] && num[i] > num[t] + 1) {
                num[i] = num[t] + 1;
                path1[i] = t;
            }
        }
    }
    printf("Time = %d",cost[destination]);
    if(!equals(destination)) {
        printf(": ");
        getpath2(destination);
        printf("\n");
    }
    else {
        printf("; ");
    }
    printf("Distance = %d: ",dis[destination]);
    getpath1(destination);
}