06-2. 旅游规划(25)最短路径
2015-01-05 21:20
513 查看
06-2. 旅游规划(25)
时间限制200 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
有了一张自驾旅游路线图,你会知道城市间的高速公路长度、以及该公路要收取的过路费。现在需要你写一个程序,帮助前来咨询的游客找一条出发地和目的地之间的最短路径。如果有若干条路径都是最短的,那么需要输出最便宜的一条路径。
输入格式说明:
输入说明:输入数据的第1行给出4个正整数N、M、S、D,其中N(2<=N<=500)是城市的个数,顺便假设城市的编号为0~(N-1);M是高速公路的条数;S是出发地的城市编号;D是目的地的城市编号。随后的M行中,每行给出一条高速公路的信息,分别是:城市1、城市2、高速公路长度、收费额,中间用空格分开,数字均为整数且不超过500。输入保证解的存在。
输出格式说明:
在一行里输出路径的长度和收费总额,数字间以空格分隔,输出结尾不能有多余空格。
样例输入与输出:
序号 | 输入 | 输出 |
1 | 4 5 0 3 0 1 1 20 1 3 2 30 0 3 4 10 0 2 2 20 2 3 1 20 | 3 40 |
2 | 2 1 0 1 1 0 2 3 | 2 3 |
#include <iostream> #include <algorithm> #include <string> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <vector> #include<queue> #include<stack> #include<map> #include<set> using namespace std; #define lson rt<<1,l,MID #define rson rt<<1|1,MID+1,r //#define lson root<<1 //#define rson root<<1|1 #define MID ((l+r)>>1) typedef long long ll; typedef pair<int,int> P; const int maxn=505; const int base=1000; const int inf=999999; const double eps=1e-5; struct edge { int from,to,cost,dis; }; edge es[505*505]; int d[maxn]; int cost[maxn]; int n,m; void short_path(int s)//bellman_ford算法 { fill(d,d+n,inf); fill(cost,cost+n,inf); d[s]=0; cost[s]=0; while(true) { bool update=false; for(int i=0;i<(m<<1);i++) { edge e=es[i]; if(d[e.from]!=inf) { if(d[e.to]>d[e.from]+e.dis) { d[e.to]=d[e.from]+e.dis; update=true; } else if(d[e.to]==d[e.from]+e.dis&&cost[e.from]!=inf&&cost[e.to]>cost[e.from]+e.cost) { cost[e.to]=cost[e.from]+e.cost; update=true; } } } if(!update)break; } } int main() { int i,j,k,t; int s,e; cin>>n>>m>>s>>e; for(i=0;i<(m<<1);i+=2) { cin>>es[i].from>>es[i].to>>es[i].dis>>es[i].cost; es[i+1].to=es[i].from; es[i+1].from=es[i].to; es[i+1].cost=es[i].cost; es[i+1].dis=es[i].dis; } short_path(s); printf("%d %d\n",d[e],cost[e]); return 0; }
相关文章推荐
- PAT 数据结构 06-图5. 旅游规划(25)Dijkstra最短路径算法
- PAT 1003. Emergency (25)(俩点间最短路径)
- 1003. Emergency (25):简单最短路径问题
- PAT (Advanced Level) 1003. Emergency (25) 最短路径 DFS
- 算法导论25(所有结点对的最短路径问题)
- 算法导论 ch25 每对顶点间的最短路径
- 06-4. How Long Does It Take (25)拓扑排序 求关键路径的最长的长度
- PAT甲题题解-1003. Emergency (25)-最短路径+路径数目
- 【PAT】1003. Emergency (25) DFS 最短路径及最短路径数
- 06-图5. 旅游规划(25)
- 从零开始最短路径学习Hadoop之06----构建Hadoop集群
- [PTA]5-25 Harry Potter's Exam (25分) (最短路径)
- 2011-02-28 CLRS Chapter25 All-Pairs Shortest Paths 每对顶点间的最短路径
- [PAT甲级]1003. Emergency (25)(求城市间最短路径和最大救援队数目)
- 【算法设计与分析基础】25、单起点最短路径的dijkstra算法
- 06-2. 旅游规划(25)MOOC
- 算法导论笔记:25所有节点对的最短路径问题
- 25.单源最短路径 Dijkstra
- 图的最短路径问题-07-图6 旅游规划
- PAT 1003. Emergency (25)<最短路径,最大搜救人数>