PAT_A1030#Travel Plan

Source:

PAT A1030 Travel Plan (30 分)

Description:

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 (≤) is the number of cities (and hence the cities are numbered from 0 to N1); 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

Keys:

Code:

 1 /*
 2 Data: 2019-06-19 14:09:44
 3 Problem: PAT_A1030#Travel Plan
 4 AC: 28:02
 5 
 6 题目大意:
 7 求花费最小的最短路径
 8 */
 9 #include<cstdio>
10 #include<vector>
11 #include<algorithm>
12 using namespace std;
13 const int M=550,INF=1e9;
14 int grap[M][M],cost[M][M],vis[M],d[M],c[M];
15 int n,st,dt,pre[M];
16 
17 void Dijskra(int s)
18 {
19     for(int i=0; i<n; i++)
20         pre[i]=i;
21     fill(vis,vis+M,0);
22     fill(d,d+M,INF);
23     fill(c,c+M,INF);
24     d[s]=0;
25     c[s]=0;
26     for(int i=0; i<n; i++)
27     {
28         int u=-1,Min=INF;
29         for(int j=0; j<n; j++)
30         {
31             if(vis[j]==0 && d[j]<Min)
32             {
33                 u = j;
34                 Min = d[j];
35             }
36         }
37         if(u==-1)   return;
38         vis[u]=1;
39         for(int v=0; v<n; v++)
40         {
41             if(vis[v]==0 && grap[u][v]!=INF)
42             {
43                 if(d[u]+grap[u][v] < d[v])
44                 {
45                     d[v] = d[u]+grap[u][v];
46                     c[v] = c[u]+cost[u][v];
47                     pre[v]=u;
48                 }
49                 else if(d[u]+grap[u][v]==d[v] && c[u]+cost[u][v]<c[v])
50                 {
51                     c[v] = c[u]+cost[u][v];
52                     pre[v]=u;
53                 }
54             }
55         }
56     }
57 }
58 
59 int main()
60 {
61 #ifdef ONLINE_JUDGE
62 #else
63     freopen("Test.txt", "r", stdin);
64 #endif // ONLINE_JUDGE
65 
66     fill(grap[0],grap[0]+M*M,INF);
67     fill(cost[0],cost[0]+M*M,INF);
68 
69     int m,v1,v2;
70     scanf("%d%d%d%d", &n,&m,&st,&dt);
71     for(int i=0; i<m; i++)
72     {
73         scanf("%d%d",&v1,&v2);
74         scanf("%d%d", &grap[v1][v2],&cost[v1][v2]);
75         cost[v2][v1]=cost[v1][v2];
76         grap[v2][v1]=grap[v1][v2];
77     }
78     Dijskra(dt);
79     int s=st;
80     while(st != dt)
81     {
82         printf("%d ", st);
83         st = pre[st];
84     }
85     printf("%d %d %d", dt,d[s],c[s]);
86 
87     return 0;
88 }

 

posted @ 2019-06-19 14:51  林東雨  阅读(226)  评论(0编辑  收藏  举报