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. Laravel开发:Laravel核心——服务容器的细节特性

    前言 在前面几个博客中,我详细讲了 Ioc 容器各个功能的使用.绑定的源码.解析的源码,今天这篇博客会详细介绍 Ioc 容器的一些细节,一些特性,以便更好地掌握容器的功能. 注:本文使用的测试类与测试 ...

  2. idangerous swiper

    最近使用Swipe.js,发现中文的资料很少,试着翻译了一下.能力有限,翻译难免错漏,欢迎指出,多谢! 翻译自:http://www.idangero.us/sliders/swiper/api.ph ...

  3. 关于logback

    1 logback是一个日志框架 2 logback的构成 LogBack被分为3个组件,logback-core, logback-classic 和 logback-access. 其中logba ...

  4. Hadoop初体验(续)--YARN

    1.Hadoop已经安装完成并启动成功 复制mapred-site.xml.template重命名为mapred-site.xml /etc/hadoop/mapred-site.xml.templa ...

  5. mac上傻瓜式java安装环境配置

    适用于mac新手用户或者黑苹果用户 首先,打开终端 输入 java -version 检查是否已安装好Java运行环境 显示我现在电脑没有安装 如果返回版本号,说明运行环境成功 对于windows用过 ...

  6. Flex自定义组件开发

    一般情况下需要组件重写都是由于以下2个原因:1.在FLEX已有组件无法满足业务需求,或是需要更改其可视化外观等特性时,直接进行继承扩展.2.为了模块化设计或进一步重用,需要对FLEX组件进行组合.而F ...

  7. php 图片下载

    php图片保存.下载 <?php //获取图片2进制内容 ,可以保存入数据库 $imgStr = file_get_contents('http://.../1.jpg'); //保存图片 $f ...

  8. Kattis - flippingcards 【并查集】

    题意 给出 N 对 数字 然后 每次从一对中 取出一个数字 判断 能否有一种取出的方案 取出的每个数字 都是不同的 思路 将每一对数字 连上一条边 然后 最后 判断每一个连通块里面 边的个数 是否 大 ...

  9. [转]Homebrew 卸载时出现:Failed to locate Homebrew! 错误

    今天在 MacBook 上安装 Homebrew,结果中间断了网,想重新卸载重装,结果一直卸载失败.问题现象如下: 问题现象 卸载时错误如下: ruby -e "$(curl -fsSL h ...

  10. 详谈 MySQL Online DDL

    作为一名DBA,对数据库进行DDL操作非常多,如添加索引,添加字段等等.对于MySQL数据库,DDL支持的并不是很好,一不留心就导致了全表被锁,经常搞得刚入门小伙伴很郁闷又无辜,不是说MySQL支持O ...