Choose the best route
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 10750 Accepted Submission(s): 3477
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时间内到达题目大意:一个人要坐车去他朋友家,但坐车呕吐,所以想在最短的时间内到达。测试数据意思:第一行三个数:n(车站的个数,n<1000),m(代表车站之间所有线路的总个数),s(代表离朋友家最近的车站)下面有m行:p q t 意思是:一条从p到q的线路,花费t时间m行之后有个数字:w(代表可以在开始时搭乘的车站)下面w个数W1,W2,,,,Ww就是车站的编号【解题思路】这道题有多个起点,一个终点,我们可以反过来考虑,把终点看做起点,起点看成终点,比较一下取用时最短的路径所用的时间为minn。【代码】dijkstra:#include<stdio.h> #include<string.h> #include<algorithm> #define N 1010 #define INF 0x3f3f3f3f using namespace std; int d[N],used[N],cost[N][N],n,m,s; void dijkstra(int s) { memset(used,0,sizeof(used)); for(int i=1;i<=n;i++){ d[i]=INF; //used[i]=0; } d[s]=0; for(int i=1;i<=n;i++) { int v=-1; for(int u=1;u<=n;u++) if(!used[u]&&(v==-1||d[u]<d[v])) v=u; if(v==-1) break; used[v]=1; for(int u=1;u<=n;u++) d[u]=min(d[u],d[v]+cost[v][u]); } } int main() { int i,j; int p,q,t,w,a,minn; while(scanf("%d%d%d",&n,&m,&s)!=EOF) { //memset(d,0,sizeof(d)); for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { //if(i==j) cost[i][j]=0; //else cost[i][j]=INF; } } for(i=1;i<=m;i++) { scanf("%d%d%d",&p,&q,&t); if(t<cost[q][p]) cost[q][p]=t;//因为是把S(目的地)看成源点,所以有向图方向要反过来 } dijkstra(s); scanf("%d",&w); minn=INF; for(i=0;i<w;i++) { scanf("%d",&a); if(d[a]<minn) minn=d[a]; } if(minn!=INF) printf("%d\n",minn); else printf("-1\n"); } return 0; }
SPFA:#include<stdio.h> #include<string.h> #include<queue> #include<stack> #include<algorithm> #define INF 0x3f3f3f3f using namespace std; int head[1000010],cnt; struct Edge { int u,v,w,next; }edge[1000010]; void add(int u,int v,int w) { edge[cnt].u=u; edge[cnt].v=v; edge[cnt].w=w; edge[cnt].next=head[u]; head[u]=cnt++; } int vis[1010]; int dist[1010]; void SPFA(int s) { queue<int>q; memset(dist,INF,sizeof(dist)); memset(vis,0,sizeof(vis)); q.push(s); dist[s]=0; vis[s]=1; while(!q.empty()) { int u=q.front(); q.pop(); vis[u]=0; for(int i=head[u];i!=-1;i=edge[i].next) { int v=edge[i].v; if(dist[v]>dist[u]+edge[i].w) { dist[v]=dist[u]+edge[i].w; if(!vis[v]) { vis[v]=1; q.push(v); } } } } } int main() { int n,m,s,p,q,t,w,a,minn; while(scanf("%d%d%d",&n,&m,&s)!=EOF) { cnt=0; memset(head,-1,sizeof(head)); for(int i=1;i<=m;i++) { scanf("%d%d%d",&p,&q,&t); add(q,p,t); } SPFA(s); scanf("%d",&w); minn=INF; while(w--) { scanf("%d",&a); if(dist[a]<minn) minn=dist[a]; } if(minn!=INF) printf("%d\n",minn); else printf("-1\n"); } return 0; }