1030 Travel Plan (30)(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算法,距离相同情况下,选消费少的,记录路径即可。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#define inf 0x3f3f3f3f
#define MAX 501
using namespace std;
int n,m,s,d,tdis,tcost,a,b;
int edis[MAX][MAX];
int ecost[MAX][MAX];
int dis[MAX];
int cost[MAX];
int vis[MAX];
int path[MAX];
void print(int k) {
if(k == s)return;
print(path[k]);
printf(" %d",k);
}
int main() {
scanf("%d%d%d%d",&n,&m,&s,&d);
for(int i = ;i < n;i ++) {
for(int j = ;j < n;j ++)
edis[i][j] = inf;
edis[i][i] = ;
dis[i] = inf;
}
for(int i = ;i < m;i ++) {
scanf("%d%d%d%d",&a,&b,&tdis,&tcost);
edis[a][b] = edis[b][a] = tdis;
ecost[a][b] = ecost[b][a] = tcost;
}
dis[s] = ;
int t,mi;
while() {
t = -;
mi = inf;
for(int i = ;i < n;i ++) {
if(!vis[i] && dis[i] < mi)mi = dis[i],t = i;
}
if(t == -)break;
vis[t] = ;
for(int i = ;i < n;i ++) {
if(vis[i] || edis[t][i] == inf)continue;
if(mi + edis[t][i] < dis[i]) {
dis[i] = mi + edis[t][i];
cost[i] = cost[t] + ecost[t][i];
path[i] = t;
}
else if(mi + edis[t][i] == dis[i] && cost[t] + ecost[t][i] < cost[i]) {
cost[i] = cost[t] + ecost[t][i];
path[i] = t;
}
}
}
printf("%d",s);
print(d);
printf(" %d %d",dis[d],cost[d]);
}
1030 Travel Plan (30)(30 分)的更多相关文章
- 1030 Travel Plan (30 分)
1030 Travel Plan (30 分) A traveler's map gives the distances between cities along the highways, toge ...
- 1030 Travel Plan (30 分)(最短路径 and dfs)
#include<bits/stdc++.h> using namespace std; ; const int inf=0x3f3f3f3f; int mp[N][N]; bool vi ...
- PAT 甲级 1030 Travel Plan (30 分)(dijstra,较简单,但要注意是从0到n-1)
1030 Travel Plan (30 分) A traveler's map gives the distances between cities along the highways, to ...
- [图算法] 1030. Travel Plan (30)
1030. Travel Plan (30) A traveler's map gives the distances between cities along the highways, toget ...
- PAT 1030 Travel Plan[图论][难]
1030 Travel Plan (30)(30 分) A traveler's map gives the distances between cities along the highways, ...
- 1030 Travel Plan (30分)(dijkstra 具有多种决定因素)
A traveler's map gives the distances between cities along the highways, together with the cost of ea ...
- 【PAT甲级】1030 Travel Plan (30 分)(SPFA,DFS)
题意: 输入N,M,S,D(N,M<=500,0<S,D<N),接下来M行输入一条边的起点,终点,通过时间和通过花费.求花费最小的最短路,输入这条路径包含起点终点,通过时间和通过花费 ...
- 1030. Travel Plan (30)
时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A traveler's map gives the dista ...
- PAT Advanced 1030 Travel Plan (30) [Dijkstra算法 + DFS,最短路径,边权]
题目 A traveler's map gives the distances between cities along the highways, together with the cost of ...
随机推荐
- cf 251 B Playing with Permutations 暴力 分类讨论
题链;http://codeforces.com/problemset/problem/251/B B. Playing with Permutations time limit per test 2 ...
- Python一些基础练习题
可变的数据类型:list, dict, set(可修改其中的元素) 不可变的数据类型:str, tuple 重点:str, list, dict (1).推导式练习 # 利用列表推导式: 找出100以 ...
- Unity 插件收集(持续更新)
MGS Machinery Unity绑定机械关节,铰链,机构插件包. MGS Mechanical Drive 用于绑定场景中的机械驱动器的Unity插件 Unity Wave Propa ...
- Black And White(DFS+剪枝)
Black And White Time Limit: 2000/2000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others ...
- 我为什么选择采用node.js来做新一代的EasyDarwin RTSP开源流媒体服务器
在去年我们还未开始开发基于node.js的新版本EasyDarwin RTSP开源流媒体服务器的时候,我写了一篇博客<对EasyDarwin开源项目后续发展的思考:站在巨人的肩膀上再跳上另一个更 ...
- Chrome Native Messaging 与本地程序之间的通信
最近项目上出现了web打印不稳定的问题,师父决定web调用本地打印程序,在查阅了相关资料和加了几个相关群咨询后得知新版的chrome不支持NNAPI了,最好用Native Messaging来处理,经 ...
- TFS中工作项的定制- 字段功能定义
参考,翻译此页面All FIELD XML Elements Reference(http://msdn.microsoft.com/en-us/library/ms194953.aspx) 对于每一 ...
- how to add them, how to multiply them
http://www.physics.miami.edu/~nearing/mathmethods/operators.pdf
- Hadoop初体验(续)--YARN
1.Hadoop已经安装完成并启动成功 复制mapred-site.xml.template重命名为mapred-site.xml /etc/hadoop/mapred-site.xml.templa ...
- server.xml笔记
本文总结自: http://www.importnew.com/26156.html 核心元素: 顶层元素: server service 连接器: connector 容器: engine > ...