题目:
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
解题思路:
这道题和1003 Emergency中的题目差不多,都是求最短路径的问题,本题和1003的区别是需要输出路径经过的结点,所需的总距离以及花费的最短开销。
求最短路径还是运用dijkstra算法,每次找出dist[]中的最小值,然后收录到collected[]中,根据收录的结点再重新更新dist距离,cost开销以及父节点。
最后通过dfs来输出路径经过的结点即可。
代码如下:
#include<stdio.h>
#include<algorithm>
#include<vector>
#define INFINITY 0x3f3f3f
using namespace std;
int G[502][502], costs[502][502];
int parent[502], dist[502], cost[502];//parent[]记录父节点,dist[]为需要的最短距离,cost[]为花费的最小开销。
bool collected[502] = { false };//记录收录的结点
vector<int> path;
int N, M, S, D;
int FindMinDist() {
int MinDist = INFINITY;
int Min = -1;
for (int i = 0; i < N; i++) {
if (collected[i] == false && dist[i] < MinDist) {
MinDist = dist[i];
Min = i;
}
}
if (Min == -1)
return -1;
else
return Min;
}
void dijkstra(int s) {
fill(parent, parent + 501, -1);
fill(dist, dist + 501, INFINITY);
fill(cost, cost + 501, INFINITY);
dist[s] = 0;
cost[s] = 0;
while (1) {
int V;
/* V = 未被收录顶点中dist最小者 */
V = FindMinDist();
if (V == -1)
break;
collected[V] = true;
for (int i = 0; i < N; i++) {
if (collected[i] == false && G[V][i] < INFINITY) {
if (G[V][i] + dist[V] < dist[i]) {//更新结点距离
dist[i] = G[V][i] + dist[V];
cost[i] = costs[V][i] + cost[V];
parent[i] = V;
}
if (G[V][i] + dist[V] == dist[i]) {//距离相等的情况下,选出开销较小的一条路径
if (costs[V][i] + cost[V] < cost[i]) {
cost[i] = costs[V][i] + cost[V];
parent[i] = V;
}
}
}
}
}
}
void dfs(int u) {
path.push_back(u);
if (parent[u] == -1)
return;
else
dfs(parent[u]);
}
int main() {
fill(G[0], G[0] + 502*502, INFINITY);
fill(costs[0], costs[0] + 502*502, INFINITY);
int u, v, dis, cst;
scanf("%d%d%d%d", &N, &M, &S, &D);
for (int i = 0; i < N; i++) {
G[i][i] = 0;
}
for (int i = 0; i < M; i++) {
scanf("%d%d%d%d", &u, &v, &dis, &cst);
G[u][v] = dis;
G[v][u] = dis;
costs[u][v] = cst;
costs[v][u] = cst;
}
dijkstra(S);
dfs(D);
reverse(path.begin(),path.end());
for (int i = 0; i < path.size(); i++)
printf("%d ", path[i]);
printf("%d %d", dist[D], cost[D]);
}