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 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 #include <iostream>
 2 #include <vector>
 3
 4 using namespace std;
 5
 6 const int INF = 0x7fffffff;
 7 struct Node
 8 {
 9     int minLength = INF;
10     bool visited = false;
11     int pre;
12     int cost;
13 };
14
15 struct Way
16 {
17     int length = INF;
18     int cost = INF;
19 };
20
21 Node nodes[500];
22 Way cityMap[500][500];
23
24 int main()
25 {
26     int cityNum, wayNum, start, end;
27     cin >> cityNum >> wayNum >> start >> end;
28
29     for (int i = 0; i < wayNum; i++)
30     {
31         int c1, c2, dst, cost;
32         cin >> c1 >> c2 >> dst >> cost;
33         cityMap[c1][c2].length = cityMap[c2][c1].length = dst;
34         cityMap[c1][c2].cost = cityMap[c2][c1].cost = cost;
35     }
36
37     //using dijkstra algorithm
38     nodes[start].minLength = 0;
39     nodes[start].visited = true;
40     nodes[start].cost = 0;
41     int min = start;
42     while (1)
43     {
44         for (int i = 0; i < cityNum; i++)
45         {
46             //update the nodes information
47             if (cityMap[min][i].length < INF && !nodes[i].visited)
48             {
49                 if (nodes[min].minLength + cityMap[min][i].length < nodes[i].minLength)
50                 {
51                     nodes[i].minLength = nodes[min].minLength + cityMap[min][i].length;
52                     nodes[i].pre = min;
53                     nodes[i].cost = nodes[min].cost + cityMap[min][i].cost;
54                 }
55                 else if (nodes[min].minLength + cityMap[min][i].length == nodes[i].minLength)
56                 {
57                     if (nodes[min].cost + cityMap[min][i].cost < nodes[i].cost)
58                     {
59                         nodes[i].pre = min;
60                         nodes[i].cost = nodes[min].cost + cityMap[min][i].cost;
61                     }
62                 }
63             }
64         }
65
66         //find the next minimum nodes
67         int minLength = INF;
68         for (int i = 0; i < cityNum; i++)
69         {
70             if (!nodes[i].visited && minLength > nodes[i].minLength)
71             {
72                 minLength = nodes[i].minLength;
73                 min = i;
74             }
75         }
76         if (minLength == INF)
77             break;
78         nodes[min].visited = true;
79     }
80
81     int totalCost = 0, totalDst = 0;
82     vector<int> vec;
83     int i = end;
84     vec.push_back(end);
85     while (1)
86     {
87         int pre = nodes[i].pre;
88         totalCost += cityMap[pre][i].cost;
89         totalDst += cityMap[pre][i].length;
90         i = pre;
91         vec.push_back(i);
92         if (i == start)
93             break;
94     }
95     for (vector<int>::reverse_iterator it = vec.rbegin(); it != vec.rend(); it++)
96         cout << *it << " ";
97     cout << totalDst << " " << totalCost;
98 }
时间: 2024-10-16 22:37:43

PAT 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

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

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

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 1030 Travel Plan

#include <cstdio> #include <cstdlib> #include <vector> #include <queue> #include <unordered_map> #include <algorithm> #include <climits> #define CMB(ID1, ID2) (((ID1)<<9) | (ID2)) using namespace std; class

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