Choose the best route
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 10601 Accepted Submission(s): 3415
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 5 1 2 2 1 5 3 1 3 4 2 4 7 2 5 6 2 3 5 3 5 1 4 5 1 2 2 3 4 3 4 1 2 3 1 3 4 2 3 2 1 1
Sample Output
1-1
//hdu2680-choose the best route (dijkstra求最短路径) //题目大意: // 给你W个起点,一个终点 s,求从这些起点到终点s所需的最短时间; //解题思路: // 由于是多起点而只有一个终点,为了节省时间我们可以逆着用迪杰斯特拉解题(即以终点s为起点,而把w个起点当做终点) //此时,由原来的p -> q就变成了 q ->p;还有就是本题处理的图是一个有向图(所以对map[p][q]直接赋一个大数INF //而不对其在进行赋权值只对map[q][p]赋权值即可) //最后,如何来区分题中给的图是有向还是无向呢?(假设p,q是图得两个顶点) //主要注意题中叙述“从一顶点与另一顶点的权值”的方式,例如有些题是求从p点到q点的距离是 c; //那这就是有向图;如果所求P点和q点之间的距离是 c(没有明确的方向词语如“到”,“去”“to”"go"等);那这就是无向图; //对于有向图,只对从p到q赋权值即map[p][q]=c;而map[q][p]=INF; //对于无向图则需都赋权值; #include<stdio.h> #include<string.h> #define INF 0xfffffff int map[1001][1001],vis[1001],d[1001]; int n,m,s,w; int stat[1001]; void dijkstra(int st) { int u,v,i; for(i=1;i<=n;i++){ d[i]=INF; vis[i]=0; } d[st]=0; while(1){ v=-1; for(u=1;u<=n;u++){ if(!vis[u]&&(v==-1||d[u]<d[v])) v=u; } if(v==-1) break; vis[v]=1; for(u=1;u<=n;u++){ if(d[u]>(d[v]+map[v][u])) d[u]=d[v]+map[v][u]; } } } int main() { int i,j,k,p,q,t,w,tim; while(scanf("%d%d%d",&n,&m,&s)!=EOF){ k=0; memset(d,0,sizeof(d)); memset(stat,0,sizeof(stat)); for(i=1;i<=n;i++) //初始化; for(j=1;j<=n;j++){ map[i][j]=INF; } for(i=1;i<=m;i++){ scanf("%d%d%d",&p,&q,&t); if(map[q][p]>t) //去重边; map[q][p]=t; } scanf("%d",&w); for(j=1;j<=w;j++){ scanf("%d",&stat[j]); if(s==stat[j]) k=1; } tim=INF; dijkstra(s); for(j=1;j<=w;j++) if(tim>d[stat[j]]) tim=d[stat[j]]; if(tim!=INF) printf("%d\n",tim); else printf("-1\n"); } return 0; }