PAT-A-1030. Travel Plan (30)-2
来源:互联网 发布:食物热量软件 编辑:程序博客网 时间:2024/05/21 09:22
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 Input4 5 0 30 1 1 201 3 2 300 3 4 100 2 2 202 3 1 20Sample 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-A-1030. Travel Plan (30)-2
- PAT A 1030. Travel Plan (30)
- PAT(A) - 1030. Travel Plan (30)
- PAT-A-1030. Travel Plan (30)
- 1030. Travel Plan (30)-PAT
- 1030. Travel Plan (30) PAT
- PAT 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 1030. Travel Plan (30)
- PAT 1030. Travel Plan
- 【PAT】1030. Travel Plan
- 【PAT Advanced Level】1030. Travel Plan (30)
- PAT 1030. Travel Plan (30) 图论
- PAT (Advanced) 1030. Travel Plan (30)
- 【PAT甲级】1030. Travel Plan (30)
- 云计算-服务器集群技术的分类
- 拆掉OS X辅助轮
- SpringMVC中基于AOP的自定义注解记录日志
- Docker各种目录的作用
- Redis多数据中心复制管理系统—— X-Pipe
- PAT-A-1030. Travel Plan (30)-2
- 关键字之命名空间
- Hadoop 2.7.2 HA datanode 启动失败处理
- Linux — 浅析线程以及多线程的同步与互斥
- 用Android Studio开发的小技巧
- LEETCODE 37. Sudoku Solver
- 剑指offer题目描述 输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序。
- ***qwb has a lot of Coins nim博弈
- 欢迎使用CSDN-markdown编辑器