PAT_A1030#Travel Plan

博客围绕PAT A1030 Travel Plan题目展开,需编写程序帮助旅行者确定起点到终点的最短路径,若最短路径不唯一则输出成本最小的路径。给出了输入输出规格、示例输入输出,关键在于解决最短路径问题,还提供了代码来源。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 }

 

转载于:https://www.cnblogs.com/blue-lin/p/11051261.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值