标签:art int color eth travel return ecif osi font
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.
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.
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.
4 5 0 3
0 1 1 20
1 3 2 30
0 3 4 10
0 2 2 20
2 3 1 20
0 2 3 3 40
分析:水题~
方法一:本题没有涉及有多个前驱结点,直接用Dijkstra,注意用递归输出最短路径的写法即可。
1 /**
2 * Copyright(c)
3 * All rights reserved.
4 * Author : Mered1th
5 * Date : 2019-02-22-21.43.06
6 * Description : A1030
7 */
8 #include<cstdio>
9 #include<cstring>
10 #include<iostream>
11 #include<cmath>
12 #include<algorithm>
13 #include<string>
14 #include<unordered_set>
15 #include<map>
16 #include<vector>
17 #include<set>
18 using namespace std;
19 const int maxn=510;
20 const int INF=1000000000;
21 int n,m,st,ed;
22 int d[maxn],c[maxn],cost[maxn][maxn],G[maxn][maxn],pre[maxn];
23 vector<int> tempPath,path;
24 bool vis[maxn]={false};
25 void Dijkstra(int s){
26 fill(d,d+maxn,INF);
27 fill(c,c+maxn,INF);
28 d[s]=0;
29 c[s]=0;
30 for(int i=0;i<n;i++) pre[i]=i;
31 for(int i=0;i<n;i++){
32 int u=-1,MIN=INF;
33 for(int j=0;j<n;j++){
34 if(d[j]<MIN && vis[j]==false){
35 u=j;
36 MIN=d[j];
37 }
38 }
39 if(u==-1) return;
40 vis[u]=true;
41 for(int v=0;v<n;v++){
42 if(G[u][v]!=INF && vis[v]==false){
43 if(d[v]>d[u]+G[u][v]){
44 d[v]=d[u]+G[u][v];
45 c[v]=c[u]+cost[u][v];
46 pre[v]=u;
47 }
48 else if(d[v]==d[u]+G[u][v]){
49 if(c[v]>c[u]+cost[u][v]){
50 c[v]=c[u]+cost[u][v];
51 pre[v]=u;
52 }
53 }
54 }
55 }
56 }
57 }
58
59 void DFS(int s,int v){
60 if(v==st){
61 printf("%d ",st);
62 return;
63 }
64 DFS(s,pre[v]);
65 printf("%d ",v);
66 }
67
68
69 int main(){
70 #ifdef ONLINE_JUDGE
71 #else
72 freopen("1.txt", "r", stdin);
73 #endif
74 cin>>n>>m>>st>>ed;
75 int a,b,dis,cos;
76 fill(G[0],G[0]+maxn*maxn,INF);
77 for(int i=0;i<m;i++){
78 scanf("%d%d%d%d",&a,&b,&dis,&cos);
79 G[a][b]=G[b][a]=dis;
80 cost[a][b]=cost[b][a]=cos;
81 }
82 Dijkstra(st);
83 DFS(st,ed);
84 printf("%d %d\n",d[ed],c[ed]);
85 return 0;
86 }
方法二:Dijkstra+DFS写法
在写Dijkstra的时候不考虑路径的开销cost,只记录最短路径。
而在写DFS函数内再计算每条最短路径的开销,求出最小开销的最短路径并输出。
1 /**
2 * Copyright(c)
3 * All rights reserved.
4 * Author : Mered1th
5 * Date : 2019-02-22-21.43.06
6 * Description : A1030
7 */
8 #include<cstdio>
9 #include<cstring>
10 #include<iostream>
11 #include<cmath>
12 #include<algorithm>
13 #include<string>
14 #include<unordered_set>
15 #include<map>
16 #include<vector>
17 #include<set>
18 using namespace std;
19 const int maxn=510;
20 const int INF=1000000000;
21 int n,m,st,ed,minCost=INF;
22 int d[maxn],cost[maxn][maxn],G[maxn][maxn];
23 vector<int> tempPath,path;
24 vector<int> pre[maxn];
25 bool vis[maxn]={false};
26 void Dijkstra(int s){
27 fill(d,d+maxn,INF);
28 d[s]=0;
29 for(int i=0;i<n;i++){
30 int u=-1,MIN=INF;
31 for(int j=0;j<n;j++){
32 if(d[j]<MIN && vis[j]==false){
33 u=j;
34 MIN=d[j];
35 }
36 }
37 if(u==-1) return;
38 vis[u]=true;
39 for(int v=0;v<n;v++){
40 if(G[u][v]!=INF && vis[v]==false){
41 if(d[v]>d[u]+G[u][v]){
42 d[v]=d[u]+G[u][v];
43 pre[v].clear();
44 pre[v].push_back(u);
45 }
46 else if(d[v]==d[u]+G[u][v]){
47 pre[v].push_back(u);
48 }
49 }
50 }
51 }
52 }
53
54 void DFS(int v){
55 if(v==st){
56 tempPath.push_back(v);
57 int tempCost=0;
58 for(int i=tempPath.size()-1;i>0;i--){
59 int id=tempPath[i],idNext=tempPath[i-1];
60 tempCost+=cost[id][idNext];
61 }
62 if(tempCost<minCost){
63 minCost=tempCost;
64 path=tempPath;
65 }
66 tempPath.pop_back();
67 return;
68 }
69 tempPath.push_back(v);
70 for(int i=0;i<pre[v].size();i++){
71 DFS(pre[v][i]);
72 }
73 tempPath.pop_back();
74 }
75
76
77 int main(){
78 #ifdef ONLINE_JUDGE
79 #else
80 freopen("1.txt", "r", stdin);
81 #endif
82 cin>>n>>m>>st>>ed;
83 int a,b,dis,cos;
84 fill(G[0],G[0]+maxn*maxn,INF);
85 for(int i=0;i<m;i++){
86 scanf("%d%d%d%d",&a,&b,&dis,&cos);
87 G[a][b]=G[b][a]=dis;
88 cost[a][b]=cost[b][a]=cos;
89 }
90 Dijkstra(st);
91 DFS(ed);
92 for(int i=path.size()-1;i>=0;i--){
93 printf("%d ",path[i]);
94 }
95 printf("%d %d\n",d[ed],minCost);
96 return 0;
97 }
标签:art int color eth travel return ecif osi font
原文地址:https://www.cnblogs.com/Mered1th/p/10421167.html