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 ...
随机推荐
- LINUX 内核 图 外国牛人
http://duartes.org/gustavo/blog/ http://blog.csdn.net/drshenlei
- 【Hibernate学习】 ——ORM(四)再次认识实体继承
在信用办时.做失信.守信.黑名单这一块的时候.先把原来的需求看了看.紧接着就開始设计实体,这一块大部分都是同样的信息,所以就设计了一个实体,而且用一个状态标识出来是失信.守信还是黑名单. 在之后的改动 ...
- C++对象模型——关键词所带来的差异(第一章)
1.2 关键词所带来的差异 (A Keyword Distinction) 假设不是为了努力维护与C之间的兼容性.C++能够比方今更简单.举个样例,假设没有八种整数须要支持的话,overload ...
- 从头认识java-15.7 Map(3)-介绍HashMap的工作原理-get方法
接着上一章节.我们来讨论一下get方法. 1.还是利用上一章节的图 下图引用自:http://www.admin10000.com/document/3322.html 我们简单说一下步骤.就是通过h ...
- Advapi32.dll 函数接口说明
Advapi32.dll 函数接口说明 函数原型 说明 AbortSystemShutDown ...
- 二分图染色模板(P1330 封锁阳光大学)
二分图染色模板(P1330 封锁阳光大学) 题目描述 曹是一只爱刷街的老曹,暑假期间,他每天都欢快地在阳光大学的校园里刷街.河蟹看到欢快的曹,感到不爽.河蟹决定封锁阳光大学,不让曹刷街. 阳光大学的校 ...
- poj 2142
Ms. Iyo Kiffa-Australis has a balance and only two kinds of weights to measure a dose of medicine. F ...
- B1085 [SCOI2005]骑士精神 A*搜索
其实就是一个爆搜加剪枝.直接爆搜肯定不行,而A*算法则是想假如剩下都是最优的话,我当前步数还是不足以达到这个状态,那么就直接返回,因为最优状态也无法做到显然不行. 这道题可以用A*最主要就是因为有15 ...
- Spring MVC中传递json数据时显示415错误解决方法
在ajax中设置 ContentType为'application/json;charset=utf-8' 传递的data类型必须是json字符串类型:{“key”:"value" ...
- Kubernetes+Jenkins+Nexus+Gitlab进行CI/CD集成
前面已经完成了 二进制部署Kubernetes集群,下面进行CI/CD集成. 一.流程说明 应用构建和发布流程说明: 1.用户向Gitlab提交代码,代码中必须包含Dockerfile: 2.将代码提 ...