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. 在VS中自动生成NuGet包以及搭建自己的或单位内部的NuGet服务器

    关于NuGet的介绍已经很多,可以参考下面的: NuGet学习笔记(1)--初识NuGet及快速安装使用 http://kb.cnblogs.com/page/143190/ NuGet学习笔记(2) ...

  2. 九度OJ刷题报告

    从8月初到现在,已经刷了400道题,越到后面题目越难,但仍会继续努力. 现将自己所AC的代码贴到博客上整理,同时供大家交流参考. 所有代码均为本人独立完成,全部采用C语言进行编写.

  3. zookeeper curator CRUD

    目录 Curator客户端的基本操作 写在前面 1.1.1. Curator客户端的依赖包 1.1.2. Curator 创建会话 1.1.3. CRUD 之 Create 创建节点 1.1.4. C ...

  4. linux c编程:线程创建

    前面章节中介绍了进程.从这一章开始介绍线程.进程和线程的差别是什么呢: 进程是具有一定独立功能的程序关于某个数据集合上的一次运行活动,进程是系统进行资源分配和调度的一个独立单位.  线程是进程的一个实 ...

  5. 016-Spring Boot JDBC

    一.数据源装配 通过查看代码可知,默认已装配了数据源和JdbcTemplate System.out.println(context.getBean(DataSource.class)); Syste ...

  6. Linux与Windows编译器的区别

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

  7. static_func

    <?php function testing() { static $a = 1; $a *= 2; echo $a."\n"; } testing(); testing() ...

  8. [原创]java WEB学习笔记34:Session 案例 之 解决表单重复提交

    本博客为原创:综合 尚硅谷(http://www.atguigu.com)的系统教程(深表感谢)和 网络上的现有资源(博客,文档,图书等),资源的出处我会标明 本博客的目的:①总结自己的学习过程,相当 ...

  9. 【LeetCode】合并两个有序链表

    将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1->3->4 输出:1->1->2- ...

  10. java深入探究07-jdbc上

    1.连接数据库三种方式 //连接数据库的URL private String url = "jdbc:mysql://localhost:3306/day17"; // jdbc协 ...