PAT 甲级 1030. Travel Plan (30)
来源:互联网 发布:天猫和淘宝什么关系 编辑:程序博客网 时间:2024/06/08 12:53
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;int n, m, s, d;int e[510][510], dis[510], cost[510][510];vector<int> pre[510];bool visit[510];const int inf = 99999999;vector<int> path, temppath;int mincost = inf;void dfs(int v) {if (v == s) {temppath.push_back(v);int tempcost = 0;for (int i = temppath.size() - 1; i > 0; i--) {int id = temppath[i], nextid = temppath[i - 1];tempcost += cost[id][nextid];}if (tempcost < mincost) {mincost = tempcost;path = temppath;}temppath.pop_back();return;}temppath.push_back(v);for (int i = 0; i < pre[v].size(); i++)dfs(pre[v][i]);temppath.pop_back();}int main() {fill(e[0], e[0] + 510 * 510, inf);fill(dis, dis + 510, inf);scanf("%d%d%d%d", &n, &m, &s, &d);for (int i = 0; i < m; i++) {int a, b;scanf("%d%d", &a, &b);scanf("%d", &e[a][b]);e[b][a] = e[a][b];scanf("%d", &cost[a][b]);cost[b][a] = cost[a][b];}pre[s].push_back(s);dis[s] = 0;for (int i = 0; i < n; i++) {int u = -1, minn = inf;for (int j = 0; j < n; j++) {if (visit[j] == false && dis[j] < minn) {u = j;minn = dis[j];}}if (u == -1) break;visit[u] = true;for (int v = 0; v < n; v++) {if (visit[v] == false && e[u][v] != inf) {if (dis[v] > dis[u] + e[u][v]) {dis[v] = dis[u] + e[u][v];pre[v].clear();pre[v].push_back(u);}else if (dis[v] == dis[u] + e[u][v]) {pre[v].push_back(u);}}}}dfs(d);for (int i = path.size() - 1; i >= 0; i--)printf("%d ", path[i]);printf("%d %d", dis[d], mincost);return 0;}
- 【PAT甲级】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甲级 1030. Travel Plan (30)
- PAT甲级1030. Travel Plan(30)
- 1030. Travel Plan (30)-PAT甲级真题
- 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)
- 1030. Travel Plan (30)-PAT甲级真题(Dijkstra + DFS,输出路径,边权)
- PAT 1030. Travel Plan
- tfs2015 自带的2014 sql express 迁移到 sql 2014 企业版
- Python利用Socket实现文件上传和下载
- 转载:浏览器DNS缓存机制分析
- 小学生除法检验,10道题,每小题10分,被除数和除数随机产生。被除数是20~200之间的数,除数是2~9之间的一位数。用户输入答案,计算机判卷,做对了加10分,做错了不允许重做,最后输出成绩;
- 深度学习中的 Batch_Size
- PAT 甲级 1030. Travel Plan (30)
- Scrapy读取设置文件(settings.py)
- svm对偶问题
- 数据结构(八)查找
- PAT 甲级 1031. Hello World for U (20)
- 一周学习总结
- XGen 使用教程
- leetcode---restore-ip-addresses---dfs
- 欢迎使用CSDN-markdown编辑器