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 }

 

posted @ 2015-08-20 21:26  JackWang822  阅读(157)  评论(0编辑  收藏  举报