PAT_A1030#Travel Plan
Source:
Description:
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
Keys:
Code:
/*
Data: 2019-06-19 14:09:44
Problem: PAT_A1030#Travel Plan
AC: 28:02 题目大意:
求花费最小的最短路径
*/
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
const int M=,INF=1e9;
int grap[M][M],cost[M][M],vis[M],d[M],c[M];
int n,st,dt,pre[M]; void Dijskra(int s)
{
for(int i=; i<n; i++)
pre[i]=i;
fill(vis,vis+M,);
fill(d,d+M,INF);
fill(c,c+M,INF);
d[s]=;
c[s]=;
for(int i=; i<n; i++)
{
int u=-,Min=INF;
for(int j=; j<n; j++)
{
if(vis[j]== && d[j]<Min)
{
u = j;
Min = d[j];
}
}
if(u==-) return;
vis[u]=;
for(int v=; v<n; v++)
{
if(vis[v]== && grap[u][v]!=INF)
{
if(d[u]+grap[u][v] < d[v])
{
d[v] = d[u]+grap[u][v];
c[v] = c[u]+cost[u][v];
pre[v]=u;
}
else if(d[u]+grap[u][v]==d[v] && c[u]+cost[u][v]<c[v])
{
c[v] = c[u]+cost[u][v];
pre[v]=u;
}
}
}
}
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE fill(grap[],grap[]+M*M,INF);
fill(cost[],cost[]+M*M,INF); int m,v1,v2;
scanf("%d%d%d%d", &n,&m,&st,&dt);
for(int i=; i<m; i++)
{
scanf("%d%d",&v1,&v2);
scanf("%d%d", &grap[v1][v2],&cost[v1][v2]);
cost[v2][v1]=cost[v1][v2];
grap[v2][v1]=grap[v1][v2];
}
Dijskra(dt);
int s=st;
while(st != dt)
{
printf("%d ", st);
st = pre[st];
}
printf("%d %d %d", dt,d[s],c[s]); return ;
}
PAT_A1030#Travel Plan的更多相关文章
- PAT1030 Travel Plan (30)---DFS
(一)题意 题目链接:https://www.patest.cn/contests/pat-a-practise/1030 1030. Travel Plan (30) A traveler's ma ...
- PAT 1030 Travel Plan[图论][难]
1030 Travel Plan (30)(30 分) A traveler's map gives the distances between cities along the highways, ...
- 1030 Travel Plan (30 分)
1030 Travel Plan (30 分) A traveler's map gives the distances between cities along the highways, toge ...
- [图算法] 1030. Travel Plan (30)
1030. Travel Plan (30) A traveler's map gives the distances between cities along the highways, toget ...
- PAT-1030 Travel Plan (30 分) 最短路最小边权 堆优化dijkstra+DFS
PAT 1030 最短路最小边权 堆优化dijkstra+DFS 1030 Travel Plan (30 分) A traveler's map gives the distances betwee ...
- 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 ...
- HDU 4014 Jimmy’s travel plan(图计数)
Jimmy’s travel plan Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Oth ...
- 【JZOJ5081】【GDSOI2017第三轮模拟】Travel Plan 背包问题+双指针+树的dfs序
题面 100 注意到ban的只会是一个子树,所以我们把原树转化为dfs序列. 然后题目就转化为,询问一段ban的区间,之后的背包问题. 比赛的时候,我想到这里,于是就开始想区间合并,于是搞了线段树合并 ...
- PAT A 1030. Travel Plan (30)【最短路径】
https://www.patest.cn/contests/pat-a-practise/1030 找最短路,如果有多条找最小消耗的,相当于找两次最短路,可以直接dfs,数据小不会超时. #incl ...
随机推荐
- Spring MVC-集成(Integration)-集成LOG4J示例(转载实践)
以下内容翻译自:https://www.tutorialspoint.com/springmvc/springmvc_log4j.htm 说明:示例基于Spring MVC 4.1.6. 以下示例说明 ...
- 一次源码编译PHP折腾记
前言LINUX环境下编译安装是很折腾人的一件事情,如果没有C/C++功底,碰到编译器报错,肯定要抓狂了 :):),有些软件需要依赖其它库,必须先把依赖库安装好才能进行软件安装.当你学会了编译安装神技之 ...
- LeetCode258——Add Digits
Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. ...
- 怎样给你的Android 安装文件(APK)瘦身
本文源地址:怎样给你的Android 安装文件(APK)瘦身 Android的apk文件越来越大了这已经是一个不争的事实. 在Android 还是最初版本号的时候,一个app的apk文件大小也还仅仅有 ...
- poj3296--Rinse(三分)
题目链接:点击打开链接 题目大意:有一个酒桶容量为Vc.里面还有Vw的酒,如今用Vb的水去刷酒桶,每次酒桶的内壁上会留下Vr的液体,最多能够刷k次,问怎么样刷酒桶.能够让酒桶里面的就最少. 假设Vb+ ...
- 2017 Multi-University Training Contest - Team 2 &&hdu 6053 TrickGCD
TrickGCD Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total ...
- AAC帧格式及编码介绍
参考资料: AAC以adts格式封装的分析:http://wenku.baidu.com/view/45c755fd910ef12d2af9e74c.html aac编码介绍:http://wenku ...
- DCloud-MUI:窗口管理
ylbtech-DCloud-MUI:窗口管理 通过预加载解决切页白屏问题,通过封装原生动画解决SPA模式的动画卡顿 1.返回顶部 1.页面初始化 在app开发中,若要使用HTML5+扩展api,必须 ...
- 93.EXTJS Form之VTypes
转自:http://blog.sina.com.cn/s/blog_7778950d0100y2pg.html 本文我们主要探讨一下EXTJS的Form中验证的问题,可能用过EXTJS的Form的人都 ...
- Java 8 实战 P2 Functional-style data processing
目录 Chapter 4. Introducing streams Chapter 5. Working with streams Chapter 6. Collecting data with st ...