1030 Travel Plan (30 分)

1030 Travel Plan (30 分)

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

分析:水题~

方法一:本题没有涉及有多个前驱结点,直接用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 }

原文地址:https://www.cnblogs.com/Mered1th/p/10421167.html

时间: 2024-08-01 14:47:54

1030 Travel Plan (30 分)的相关文章

1030 Travel Plan (30 分)

1030 Travel Plan (30 分) 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 ci

1030 Travel Plan (30分)

最短路径问题.一定要注意将visited[u]是否等于0加入判断!很关键,还是没有理解透,所以才忘了加.(第42行和第45行) 其实图相关的题通过率蛮高的...套路题,坑少. 1 #include <iostream> 2 #include<cstdio> 3 #include<vector> 4 #define inf 0x3f3f3f3f 5 using namespace std; 6 struct Edge{ 7 int next,dis,cos; 8 }; 9

PAT 1030. Travel Plan (30)

1030. Travel Plan (30) 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 cit

1030 Travel Plan (30)(30 分)

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.

1030. Travel Plan (30)

dfs使用vector保存最短路径 时间限制 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 tr

PAT (Advanced Level) 1030. Travel Plan (30)

先处理出最短路上的边.变成一个DAG,然后在DAG上进行DFS. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<queue> #include<vector> using namespace std; const int INF=0x7FFFFFFF; const int

PAT:1030. Travel Plan (30) AC

#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; const int MAXV=510; const int INF=0x3fffffff; int N,M,S,D; //城市数,高速公路数,起点,终点 bool visit[MAXV]; //标记在迪杰斯特拉中是否遍历过 int GD[MAXV][MAXV]; //存储距离图 int GC[MAXV][MAXV];

A1030 Travel Plan (30分)

一.技术总结 这一题是关于图的遍历,但是涉及Djikstra算法,在求最短路径的同时,还要把路径记录下来:同时增加了边权,也就会每个城市之间的花费: 这里采用Djikstra算法+DFS遍历的方法 第一步使用Djikstra算法求出最短路径,使用vector类型pre数组进行存储,每个结点里面保存的是他们的前驱结点. 然后再使用DFS遍历pre,然后会有得出遍历树. 要注意下标是0~n-1,还是1~n. 细节问题参考代码 二.参考代码 #include<bits/stdc++.h> using

pat1030. Travel Plan (30)

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