1030. Travel Plan (30)

 

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

A traveler's map gives the distances between cities along the highways, together with the cost of each highway. Now you are supposed to write a program to help a traveler to decide the shortest path between his/her starting city and the destination. If such a shortest path is not unique, you are supposed to output the one with the minimum cost, which is guaranteed to be unique.

Input Specification:

Each input file contains one test case. Each case starts with a line containing 4 positive integers N, M, S, and D, where N (<=500) is the number of cities (and hence the cities are numbered from 0 to N-1); M is the number of highways; S and D are the starting and the destination cities, respectively. Then M lines follow, each provides the information of a highway, in the format:

City1 City2 Distance Cost

where the numbers are all integers no more than 500, and are separated by a space.

Output Specification:

For each test case, print in one line the cities along the shortest path from the starting point to the destination, followed by the total distance and the total cost of the path. The numbers must be separated by a space and there must be no extra space at the end of output.

Sample Input
4 5 0 3
0 1 1 20
1 3 2 30
0 3 4 10
0 2 2 20
2 3 1 20
Sample Output
0 2 3 3 40

 1 #include<stdio.h>
 2 #define MAX 510
 3 int INF = 1000000;
 4 struct Edg
 5 {
 6     int len;
 7     int cost;
 8 };
 9 
10 int d[MAX];
11 int c[MAX];
12 Edg Grap[MAX][MAX];
13 bool vist[MAX];
14 int pre[MAX];
15 
16 void Dijkstra(int begin,int NodeNum)
17 {
18     d[begin] = 0;
19     c[begin] = 0; 
20     for(int i = 0 ;i <NodeNum ;i++)
21     {
22         int index =-1;
23         int MIN = INF;
24         for(int j = 0;j < NodeNum ;j++)
25         {
26             if(!vist[j] && MIN > d[j])
27             {
28                 MIN = d[j];
29                 index = j;
30             }
31         }
32 
33         if(index == -1) return;
34 
35         vist[index] = true;
36 
37         for(int v = 0;v < NodeNum ; v++)
38         {
39             if(!vist[v] && Grap[index][v].len != INF)
40             {
41                 if(d[index]+ Grap[index][v].len < d[v])
42                 {
43                     d[v] = d[index]+ Grap[index][v].len;
44                     c[v] = c[index] + Grap[index][v].cost;
45                     pre[v] = index;
46                 }
47                 else if(d[index]+ Grap[index][v].len == d[v] && c[v] > c[index] + Grap[index][v].cost)
48                 {
49                     c[v] = c[index] + Grap[index][v].cost;
50                     pre[v] = index;
51                 }
52             }
53         }
54     }
55 
56 }
57 
58 
59 void DFS(int begin,int end)
60 {
61     if(end == begin)
62     {
63         printf("%d ",end);
64         return ;
65     }
66     DFS(begin,pre[end]);
67     printf("%d ",end);
68 }
69 
70 int main()
71 {
72     int i,j,N,M,S,D,x,y;
73     Edg Etem;
74     scanf("%d%d%d%d",&N,&M,&S,&D);
75 
76     for(i =0;i < N;i++)
77     {
78         for(j =0;j < N;j++)
79         {
80             Grap[i][j].cost = INF;
81             Grap[i][j].len = INF;
82         }
83     }
84 
85     for(i =0;i < M;i++)
86     {
87         scanf("%d%d%d%d",&x,&y,&Etem.len,&Etem.cost);
88         Grap[x][y] = Grap[y][x] = Etem;
89         d[i] = c[i] = INF;
90     }
91 
92     Dijkstra(S,N);
93 
94     DFS(S,D);
95 
96     printf("%d %d
",d[D],c[D]);
97     return 0;
98 }
原文地址:https://www.cnblogs.com/xiaoyesoso/p/4291874.html