1030 Travel Plan (30分)(dijkstra 具有多种决定因素)
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 具有多种决定因素)的更多相关文章
- 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 ...
- 【PAT甲级】1030 Travel Plan (30 分)(SPFA,DFS)
题意: 输入N,M,S,D(N,M<=500,0<S,D<N),接下来M行输入一条边的起点,终点,通过时间和通过花费.求花费最小的最短路,输入这条路径包含起点终点,通过时间和通过花费 ...
- PAT-1030 Travel Plan (30 分) 最短路最小边权 堆优化dijkstra+DFS
PAT 1030 最短路最小边权 堆优化dijkstra+DFS 1030 Travel Plan (30 分) A traveler's map gives the distances betwee ...
- [图算法] 1030. Travel Plan (30)
1030. Travel Plan (30) A traveler's map gives the distances between cities along the highways, toget ...
- 1030 Travel Plan (30)(30 分)
A traveler's map gives the distances between cities along the highways, together with the cost of ea ...
- 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 ...
- 1030. Travel Plan (30)
时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A traveler's map gives the dista ...
- PAT A 1030. Travel Plan (30)【最短路径】
https://www.patest.cn/contests/pat-a-practise/1030 找最短路,如果有多条找最小消耗的,相当于找两次最短路,可以直接dfs,数据小不会超时. #incl ...
- PAT甲题题解-1030. Travel Plan (30)-最短路+输出路径
模板题最短路+输出路径如果最短路不唯一,输出cost最小的 #include <iostream> #include <cstdio> #include <algorit ...
随机推荐
- Redis04——五分钟明白Redis的哨兵模式
和所有的数据库一样,Redis也支持集群化,Redis的集群分为分布式集群和主从集群.大部分公司采取的都是主从集群.所以在本篇文章内,我们将着重介绍Redis的主从集群及哨兵机制. 由于Redis的主 ...
- Simulink仿真入门到精通(八) M语言对Simulink模型的自动化操作及配置
8.1 M语言控制模型的仿真 M语言与Simulink结合的方式: 在Simulink模型或模块中使用回调函数 在M语言中调用与模型相关的命令,控制模型的建立,设置模块的属性,增删信号线,以及运行模型 ...
- vscode使用cnpm报错
1.在wind10搜索框里输入 Windows PowerShell 进入这个界面 2.打开Windows PowerShell 之后 输入命令:set-ExecutjionPolicy Remot ...
- JavaScript中的内存释放
C.C++语言需要手动管理内存的分配与释放(常用方法:malloc(), calloc(), realloc()和free()等).而JavaScript与Java.C#相似,内置了垃圾回收器,能自动 ...
- 【Python】2.17学习笔记 移位运算符,逻辑运算符
移位运算符 左移运算符 \(<<\),将对应的二进制数末尾补一颗零,高位自然溢出(遁入虚无 print( 5 << 2 ) 把\(5\)的二进制数左移两位 即把\(101\)变 ...
- dos下 批处理 用 pause 可以在最后暂停 查看结果信息 build.bat
dos下 批处理 用 pause 可以在最后暂停 查看结果信息
- 基于Ubuntu的ORB-SLAM2项目环境搭建过程
目录 关于ORB-SLAM2 环境搭建 已有环境 创建环境 新建项目目录 安装Pangolin 安装OpenCV 3.2 安装Eigen DBoW2 and g2o (Included in Thir ...
- 1. postman使用
postman使用教程: https://blog.csdn.net/fxbin123/article/details/80428216 http://bayescafe.com/tools/use- ...
- NFS作为根文件系统,挂载超时
NFS服务器配置正确后,使用ramfs,通过mount能够正常挂载NFS,但是作为ROOTFS无法正常挂载,显示超时. 经查看log,RPC报错-120. 分析结果: 在Ubuntu1804上,nfs ...
- Journal of Proteome Research | SAAVpedia: identification, functional annotation, and retrieval of single amino acid variants for proteogenomic interpretation | SAAV的识别、功能注释和检索 | (解读人:徐洪凯)
文献名:SAAVpedia: identification, functional annotation, and retrieval of single amino acid variants fo ...