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 (≤) 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

题目分析:单源最短路
#define _CRT_SECURE_NO_WARNINGS
#include <climits>
#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
#include<string>
#include<cmath>
using namespace std;
int N, M, S, D;
int a[][];
int b[][];
int Dist[];
int Path[];
int Collected[];
int Cost[];
void Dijsktra()
{
Dist[S] = ;
Path[S] = -;
Cost[S] = ;
for (int i = ; i < N; i++)
{
int minv=-, min = INT_MAX;
for (int v = ; v < N; v++)
{
if (!Collected[v] && min > Dist[v])
{
min = Dist[v];
minv = v;
}
}
Collected[minv] = ;
for (int j = ; j < N; j++)
{
if (!Collected[j] && a[minv][j] != INT_MAX)
{
if (Dist[minv] + a[minv][j] < Dist[j])
{
Dist[j] = Dist[minv] + a[minv][j];
Cost[j] = Cost[minv] + b[minv][j];
Path[j] = minv;
}
else if (Dist[minv] + a[minv][j] == Dist[j])
{
if (Cost[minv] + b[minv][j] < Cost[j])
{
Cost[j] = Cost[minv] + b[minv][j];
Path[j] = minv;
}
}
}
}
}
}
int main()
{
cin >> N >> M >> S >> D;
int c1, c2, d, c;
fill(a[],a[]+*,INT_MAX);
fill(b[], b[]+* , INT_MAX);
fill(Dist, Dist + , INT_MAX);
fill(Cost, Cost + , INT_MAX);
for (int i = ; i < M; i++)
{
cin >> c1 >> c2 >> d >> c;
a[c1][c2] = a[c2][c1] = d;
b[c1][c2] = b[c2][c1] = c;
}
Dijsktra();
int temp = D;
stack<int> st;
while (temp!=-)
{
st.push(temp);
temp = Path[temp];
}
while (!st.empty())
{
cout << st.top()<<" ";
st.pop();
}
cout << Dist[D] <<" "<<Cost[D];
return ;
}

1030 Travel Plan (30分)(dijkstra 具有多种决定因素)的更多相关文章

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

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

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

  3. PAT-1030 Travel Plan (30 分) 最短路最小边权 堆优化dijkstra+DFS

    PAT 1030 最短路最小边权 堆优化dijkstra+DFS 1030 Travel Plan (30 分) A traveler's map gives the distances betwee ...

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

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

  5. 1030 Travel Plan (30)(30 分)

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

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

  7. 1030. Travel Plan (30)

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

  8. PAT A 1030. Travel Plan (30)【最短路径】

    https://www.patest.cn/contests/pat-a-practise/1030 找最短路,如果有多条找最小消耗的,相当于找两次最短路,可以直接dfs,数据小不会超时. #incl ...

  9. PAT甲题题解-1030. Travel Plan (30)-最短路+输出路径

    模板题最短路+输出路径如果最短路不唯一,输出cost最小的 #include <iostream> #include <cstdio> #include <algorit ...

随机推荐

  1. 量子计算机编程(二)——QPU基础函数

    第二部分主要是QPU的基础功能,第一部分就像是我们有了哪些基本的语句,第二部分就是我们能写一些简单基础的函数,一些小模块,第三部分就是他的应用了. 先来看一下一个简单量子应用的结构: 第一步,将量子态 ...

  2. Notepad++远程连接Linux

    为方便编辑Linux上的文件,我们可以用Notepad++的NppFTP插件 工具:Notepad++.CentOS 1.通过ifconfig命令找到主机ip 2.打开Notepad++插件NppFT ...

  3. 原生js实现随着滚动条滚动,导航会自动切换的效果

    最近学习前端,把前面用原生js写的一段有关tab切换效果的代码贴上,实现的效果比较简陋,请大家见谅 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1 ...

  4. List remove ConcurrentModificationException源码分析

    代码块 Java         Exception in thread "main" java.util.ConcurrentModificationException at j ...

  5. Python1-变量

    一.变量和简单数据类型 1.变量 命名规则: 变量名组成——字母.下划线.数字(不可做首字符): 用下划线分隔单词,不可包含空格: 关键字和函数名不可做变量名: 小写字母做变量名,不建议使用大小写的字 ...

  6. Spark入门(二)--如何用Idea运行我们的Spark项目

    用Idea搭建我们的Spark环境 用IDEA搭建我们的环境有很多好处,其中最大的好处,就是我们甚至可以在工程当中直接运行.调试我们的代码,在控制台输出我们的结果.或者可以逐行跟踪代码,了解spark ...

  7. qt creator源码全方面分析(3-5)

    目录 qtcreatorlibrary.pri 使用实例 上半部 下半部 结果 qtcreatorlibrary.pri 上一章节,我们介绍了src.pro,这里乘此机会,把src目录下的所有项目文件 ...

  8. requests模块使用二

    1.cookies和session 1.1.什么是cookie和session? cookie是网站用来辨别用户身份,进行会话跟踪,存储在本地终端上的数据. session(会话)起来本含义是指有始有 ...

  9. Nvue/Weex

    Nvue/Weex 使用Uniapp做了一个App,感觉性能不是很好,了解过Uniapp的Nvue,就想做一个纯Nvue项目,其实基本就是做一个Weex项目,不得不说坑是真的多,但是渲染性能真的是没得 ...

  10. CF1327A Sum of Odd Integers 题解

    原题链接 简要题意: 多组数据,问能否把 \(n\) 分为 \(k\) 个 不同的 正奇数之和. 盲猜数学结论题. 只要考虑两个问题: \(n\) 的大小是否足够. \(n\) 的奇偶性是否满足. 对 ...