最短路
来源:互联网 发布:star rating.js 编辑:程序博客网 时间:2024/04/30 17:04
Choose the best route
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 13 Accepted Submission(s) : 6
Problem Description
One day , Kiki wants to visit one of her friends. As she is liable to carsickness , she wants to arrive at her friend’s home as soon as possible . Now give you a map of the city’s traffic route, and the stations which are near Kiki’s home so that she can take. You may suppose Kiki can change the bus at any station. Please find out the least time Kiki needs to spend. To make it easy, if the city have n bus stations ,the stations will been expressed as an integer 1,2,3…n.
Input
There are several test cases.
Each case begins with three integers n, m and s,(n<1000,m<20000,1=<s<=n) n stands for the number of bus stations in this city and m stands for the number of directed ways between bus stations .(Maybe there are several ways between two bus stations .) s stands for the bus station that near Kiki’s friend’s home.
Then follow m lines ,each line contains three integers p , q , t (0<t<=1000). means from station p to station q there is a way and it will costs t minutes .
Then a line with an integer w(0<w<n), means the number of stations Kiki can take at the beginning. Then follows w integers stands for these stations.
Each case begins with three integers n, m and s,(n<1000,m<20000,1=<s<=n) n stands for the number of bus stations in this city and m stands for the number of directed ways between bus stations .(Maybe there are several ways between two bus stations .) s stands for the bus station that near Kiki’s friend’s home.
Then follow m lines ,each line contains three integers p , q , t (0<t<=1000). means from station p to station q there is a way and it will costs t minutes .
Then a line with an integer w(0<w<n), means the number of stations Kiki can take at the beginning. Then follows w integers stands for these stations.
Output
The output contains one line for each data set : the least time Kiki needs to spend ,if it’s impossible to find such a route ,just output “-1”.
Sample Input
5 8 51 2 21 5 31 3 42 4 72 5 62 3 53 5 14 5 122 34 3 41 2 31 3 42 3 211
Sample Output
1-1
#include<stdio.h>#include<string.h>#define maxint 0xfffffff#define max 1110int n,m,s,big;int map[max][max];int vis[max];int dis[max];void init(){ int i,j; for(i=1; i<=max; i++) for(j=1; j<=max; j++) map[i][j]=maxint;}void Dijkstra(int tem){ int i,j; for(i=1; i<=n; i++) { vis[i]=0; dis[i]=map[tem][i]; } vis[tem]=1; dis[tem]=0; for(i=1; i<n; i++) { int d=maxint; int mid=0; for(j=1; j<=n; j++) { if(vis[j]==0&&dis[j]<d) { d=dis[j]; mid=j; } } vis[mid]=1; if(d==maxint) break; for(j=1; j<=n; j++) { if(vis[j]==0&&map[mid][j]<maxint&&map[mid][j]+d<dis[j]) dis[j]=map[mid][j]+d; } }}int main(){ int a,b,d,mm,te[max]; while(scanf("%d%d%d",&n,&m,&s)!=EOF) { big=0; init(); for(int i=0; i<m; i++) { scanf("%d%d%d",&a,&b,&d); if(big<a) big=a; if(big<b) big=b; if(map[b][a]>d) map[b][a]=d; } scanf("%d",&mm); int ans=maxint; for(int i=1; i<=mm; i++) { scanf("%d",&te[i]); } if(big<s) { printf("-1\n"); continue; } Dijkstra(s); for(int i=1; i<=mm; i++) if(dis[te[i]]<ans) ans=dis[te[i]]; if(ans!=maxint) printf("%d\n",ans); else printf("-1\n"); } return 0;}
- 最短路 & 次短路
- 最短路
- 最短路
- 最短路
- 最短路
- 最短路
- 最短路
- 最短路
- 最短路
- 最短路
- 最短路
- 最短路
- 最短路
- 最短路
- 最短路
- 最短路
- 最短路
- 最短路
- Python笔记——几种数据结构的实现:栈、队列及二叉树
- 最短路
- Python笔记——排序算法的实现
- SQL Server 2005新特性之使用with关键字解决递归父子关系(详细)
- BIOS入口地址:FFFF:0000是指什么意思?在物理上是指内存条的什么位置?
- 最短路
- MFC下CSocket编程详解
- php5总结
- ASP.NET中将对界面的操作封装到DLL中
- SQL连接
- UML的基本构造块
- Core Architecture Blocks (核心架构模块)(二)
- 最短路
- ANDROID开发与手机USB调试--手机调试模式出不来