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 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

来源: <http://www.patest.cn/contests/pat-a-practise/1030>

  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4. int map[501][501] = { 0 };
  5. int cost[501][501] = { 0 };
  6. int mark[501] = { 0 };
  7. int n, m, s, d;
  8. int minDistance=999999;
  9. int minCost = 999999;
  10. vector<int> road, roadtemp;
  11. void dfs(int i, int distNow, int costNow, vector<int> roadNow) {
  12. roadNow.push_back(i);
  13. if (i == d&&distNow < minDistance) {
  14. minDistance = distNow;
  15. minCost = costNow;
  16. road.clear();
  17. for (int j = 0; j < roadNow.size(); j++)
  18. road.push_back(roadNow[j]);
  19. }
  20. else if (i == d&&distNow == minDistance&&costNow < minCost) {
  21. minDistance = distNow;//same as before
  22. minCost = costNow;
  23. road.clear();
  24. for (int j = 0; j < roadNow.size(); j++)
  25. road.push_back(roadNow[j]);
  26. }
  27. else if (i == d||distNow>minDistance||costNow>minCost)
  28. return;
  29. for (int j = 0; j < n; j++) {
  30. if (map[i][j] != 0 && mark[j] == 0) {
  31. mark[j] = 1;
  32. dfs(j, distNow + map[i][j], costNow + cost[i][j], roadNow);
  33. mark[j] = 0;
  34. }
  35. }
  36. }
  37. int main(void) {
  38. cin >> n >> m >> s >> d;
  39. if (s == d) {
  40. cout << s << " " << s << " 0 0";
  41. return 0;
  42. }
  43. for (int i = 0; i < m; i++) {
  44. int st, dt, dist, costt;
  45. cin >> st >> dt >> dist >> costt;
  46. map[st][dt] = map[dt][st] = dist;
  47. cost[st][dt] = cost[dt][st] = costt;
  48. }
  49. mark[s] = 1;
  50. dfs(s, 0, 0, roadtemp);
  51. for (int i = 0; i < road.size(); i++) {
  52. cout << road[i] << " ";
  53. }
  54. cout << minDistance << " " << minCost;
  55. return 0;
  56. }

来自为知笔记(Wiz)

时间: 2024-10-25 10:27:17

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

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 分)

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)(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分)

最短路径问题.一定要注意将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 (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];

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

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 (DFS)

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