PAT-A-1030. Travel Plan (30)-2
2017-06-04 23:01
381 查看
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 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
#include<iostream> #include<cstdio> #include<algorithm> #include<vector> using namespace std; const int maxn = 510; const int inf = 1000000000; int g[maxn][maxn], cost[maxn][maxn]; int d[maxn]; bool vis[maxn] = { false }; vector<int> pre[maxn]; vector<int> temp, path; int n, m, st, ed; void dijkstra(int s) { fill(d, d + maxn, inf); d[s] = 0; for (int i = 0; i < n; i++) { int u = -1, min = inf; for (int j = 0; j < n; j++) { if (vis[j] == false && d[j] < min) { u = j; min = d[j]; } } if (u == -1) return; vis[u] = true; for (int v = 0; v < n; v++) { if (vis[v] == false && g[u][v] != inf) { if (d[u] + g[u][v] < d[v]) { d[v] = g[u][v] + d[u]; pre[v].clear(); pre[v].push_back(u); } else if (d[u] + g[u][v] == d[v]) { pre[v].push_back(u); } } } } } int mincost = inf; void DFS(int v) { if (v == st) { temp.push_back(v); int tempcost = 0; for (int i = temp.size() - 1; i > 0; i--) { int id = temp[i]; int idnext = temp[i - 1]; tempcost += cost[id][idnext]; } if (tempcost < mincost) { path = temp; mincost = tempcost; } temp.pop_back(); return; } temp.push_back(v); for (int i = 0; i < pre[v].size(); i++) { DFS(pre[v][i]); } temp.pop_back(); } int main() { cin >> n >> m >> st >> ed; int u, v; fill(g[0], g[0] + maxn*maxn, inf); for (int i = 0; i < m; i++) { cin >> u >> v; cin >> g[u][v] >> cost[u][v]; g[v][u] = g[u][v]; cost[v][u] = cost[u][v]; } dijkstra(st); DFS(ed); for (int i = path.size() - 1; i >= 0; i--) { cout << path[i]<<" "; } cout << d[ed] << " " << mincost << endl; system("pause"); return 0; }
相关文章推荐
- PAT甲级1030. Travel Plan (30)
- PAT 1030. Travel Plan (30) (最短路)
- PAT 1030. Travel Plan (30)(Dijkstra,最短路径的同时计算最小奥cost)
- 【PAT Advanced Level】1030. Travel Plan (30)
- PAT 1030. Travel Plan (30)
- pat 1030. Travel Plan (30)
- PAT甲级 1030. Travel Plan (30)
- PAT甲级1030. Travel Plan(30)
- PAT_A 1030. Travel Plan (30)
- PAT 甲级 1030. Travel Plan (30)
- PAT - 甲级 - 1030. Travel Plan (30) (Dijkstra+DFS)
- 【PAT甲级】1030. Travel Plan (30)
- 1030. Travel Plan (30)-PAT甲级真题
- PAT-A-1030. Travel Plan (30)
- PAT 1030. Travel Plan (30)
- PAT甲题题解-1030. Travel Plan (30)-最短路+输出路径
- 1030. Travel Plan (30) PAT 甲级
- PAT 甲级 1030. Travel Plan (30)
- 【PAT】【Advanced Level】1030. Travel Plan (30)
- PAT (Advanced Level)1030. Travel Plan (30) dfs