当前位置: 代码迷 >> 综合 >> Choose the best route 【最短路】+【反向建图】
  详细解决方案

Choose the best route 【最短路】+【反向建图】

热度:34   发布时间:2023-11-10 05:36:26.0

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.
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个点,m条边,给定一些点,可以从这些点出发到达终点,问这其中最短的路径多少。
我们可以逆序建图,这样就是求终点到这些点中的最小距离。【求一次最短路径就可以了】
代码

#include<bits/stdc++.h>
#define LL long long
using namespace std;
const int MAXN = 1e5;
const int MAXM =1e8;
const int inf = 0x3f3f3f3f;
inline int read(){int f=1,x=0;char ch=getchar();while(ch<'0'||ch>'9') {
   if(ch=='-') f=-1;ch=getchar();}while(ch>='0'&&ch<='9') {x=x*10+ch-'0';ch=getchar();}return x*f;
}
/*------------------------------*/
struct Edge {int from,to,val,nexts;
}edge[MAXN];
int head[MAXN],top=0;
int n,m,s;
void init(){memset(head,-1,sizeof(head));top=0;
}
void addedge(int a,int b,int c){/*int i,j;for( i=head[a];i!=-1;i=edge[i].nexts){if(edge[i].to==b){edge[i^1].val=edge[i].val=min(edge[i].val,c);break;}}if(i!=-1) return ;*/Edge e={a,b,c,head[a]};edge[top]=e;head[a]=top++;
}void getmap(){while(m--){int a,b,c;a=read();b=read();c=read();addedge(b,a,c);}
}
int vis[MAXN],dis[MAXN];
void spfa(){queue<int >Q;memset(vis,0,sizeof(vis));memset(dis,0x3f,sizeof(dis));Q.push(s);vis[s]=1;dis[s]=0;while(!Q.empty()){int now=Q.front();Q.pop(); vis[now]=0;for(int i=head[now];i!=-1;i=edge[i].nexts){int nexts=edge[i].to;if(dis[nexts]>dis[now]+edge[i].val){dis[nexts]=dis[now]+edge[i].val;if(!vis[nexts]){Q.push(nexts);vis[nexts]=1;}}}}
}
void solve(){int sum=inf;int q;cin>>q;int a;for(int i=1;i<=q;i++){a=read();sum=min(sum,dis[a]);}if(sum==inf) printf("-1\n");elseprintf("%d\n",sum);
}int main(){while(~scanf("%d%d%d",&n,&m,&s)){init();getmap();spfa();solve();}return 0;
}
  相关解决方案