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 分)的更多相关文章

  1. 1030 Travel Plan (30 分)

    1030 Travel Plan (30 分) A traveler's map gives the distances between cities along the highways, toge ...

  2. 1030 Travel Plan (30 分)(最短路径 and dfs)

    #include<bits/stdc++.h> using namespace std; ; const int inf=0x3f3f3f3f; int mp[N][N]; bool vi ...

  3. 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 ...

  4. [图算法] 1030. Travel Plan (30)

    1030. Travel Plan (30) A traveler's map gives the distances between cities along the highways, toget ...

  5. PAT 1030 Travel Plan[图论][难]

    1030 Travel Plan (30)(30 分) A traveler's map gives the distances between cities along the highways, ...

  6. 1030 Travel Plan (30分)(dijkstra 具有多种决定因素)

    A traveler's map gives the distances between cities along the highways, together with the cost of ea ...

  7. 【PAT甲级】1030 Travel Plan (30 分)(SPFA,DFS)

    题意: 输入N,M,S,D(N,M<=500,0<S,D<N),接下来M行输入一条边的起点,终点,通过时间和通过花费.求花费最小的最短路,输入这条路径包含起点终点,通过时间和通过花费 ...

  8. 1030. Travel Plan (30)

    时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A traveler's map gives the dista ...

  9. 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 ...

随机推荐

  1. C++ 错误积累

    错误一 VS2012错误:不能在成员函数  的类外部重新声明该函数 解决:检查函数的大括号匹配

  2. P1009 阶乘之和

    P1009 阶乘之和 题目提供者洛谷OnlineJudge 标签数论(数学相关)高精1998NOIp提高组NOIp普及组 难度普及- 通过/提交1139/3791 提交该题 讨论 题解 记录 题目描述 ...

  3. L2 范数 L1 范数 出租车范数

    https://en.wikipedia.org/wiki/Norm_(mathematics) http://cs231n.github.io/classification/

  4. 加密php源代码

    <?php function RandAbc($length = "") { //返回随机字符串 $str = "ABCDEFGHIJKLMNOPQRSTUVWXY ...

  5. php递归删除所有文件

    function del_file($dir) { if (@rmdir($dir)==false && is_dir($dir)) { if ($dp = opendir($dir) ...

  6. 我的Android进阶之旅------>android api的源代码下载地址

    Eclipse的SDK Manager不好用,关联不到API的源代码,终于找到一个可以下载API对应的source地址: http://grepcode.com/project/repository. ...

  7. Linux与Windows编译器的区别

    移植工作開始后的第一步就是在目标平台Linux上进行编译,并链接源码.因为须要移植的软件通常并未在Linux平台上编译过,编译的过程可能会遇到非常大的困难.普通情况下,由类型声明引起的编译错误是比較e ...

  8. python利用wxpy监控微信公众号

    此次利用wxpy可以进行微信公众号的消息推送监测(代码超级简单),这样能进行实时获取链接.但是不光会抓到公众号的消息,好友的消息也会抓到(以后会完善的,毕竟现在能用了,而且做项目的微信号肯定是没有好友 ...

  9. 牛客小白月赛1 E 圆与三角形 【数学】

    题目链接 https://www.nowcoder.com/acm/contest/85/E 思路 在三角形中,这一串东西的值恒为1 又 SIN A 的最大值 为1 所以 这串式子的最大值 就是 r ...

  10. 《程序员代码面试指南》第一章 栈和队列 最大值减去最小值小于或等于num的数量

    题目 给定整数数组arr和整数num,共返回多少的数组满足如下情况 max(arr[i...j]) - min(arr[i...j]) <= num max(arr[i...j])表示数组arr ...