Choose the best route 【最短路】+【反向建图】
来源:互联网 发布:航海家软件免费版 编辑:程序博客网 时间:2024/06/08 06:33
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 longusing 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"); else printf("%d\n",sum);}int main(){ while(~scanf("%d%d%d",&n,&m,&s)){ init(); getmap(); spfa(); solve(); } return 0;}
- Choose the best route 【最短路】+【反向建图】
- HDOJ Choose the best route 2680【最短路Dijkstra+反向建图】
- HDU 2680 Choose the best route 【最短路 反向建图 dijkstra & SPFA 】
- 杭电2680-Choose the best route(最短路反向建图)
- HDU 2680 Choose the best route(最短路,反向)
- HDU 2680 Choose the best route (最短路+反向图)
- HDOJ 2680 Choose the best route(最短路—反向图,dijkstra算法)
- hdu2680最短路Choose the best route
- HDU2680 Choose the best route 最短路
- Choose the best route 2680 (dijkstra,反向建图)
- hdu 2680 Choose the best route【dijstra+反向建图】
- 【最短路+dijkstra】 2680 Choose the best route
- hdu Choose the best route(最短路 --- Dijkstra)
- hdu 2680-Choose the best route最短路dijkstra
- Hud 2680 Choose the best route[基础最短路]
- HDU 2680 Choose the best route 变形最短路 DIJKSTRA
- hdu2680 Choose the best route 最短路(Dijkstra算法)
- HDU 2680Choose the best route(dijk最短路)
- lintcode -- 丢失的第一个正整数
- windows下创建.jshintrc文件
- 单词接龙 II-LintCode
- 动态规划
- Qt中根据不同版本号使用不同源码
- Choose the best route 【最短路】+【反向建图】
- Codeforces-158B
- Android 为什么监听不到开机广播action.BOOT_COMPLETED
- cat /proc/meminfo 详细
- H264—MP4格式及在MP4文件中提取H264的SPS、PPS及码流
- AS gradle 理解
- QT sizeHint 及 Policy的用法
- 浅谈深度学习中的激活函数
- Delphi和C++builder中的MessageBox及相近的四种消息框