Choose the best route 2680 (dijkstra,反向建图)
来源:互联网 发布:传淘宝代销是什么意思 编辑:程序博客网 时间:2024/06/09 12:36
Choose the best route
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 10282 Accepted Submission(s): 3317
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>#include<algorithm>#include<math.h>#define mx 0x3f3f3f3fusing namespace std;int map[1010][1010],dis[1010],vis[1010];int n,m,s;void djs(int x){int i,j,k;memset(vis,0,sizeof(vis));for(i=1;i<=n;i++)dis[i]=mx;dis[x]=0;while(1){k=-1;for(i=1;i<=n;i++){if(!vis[i]&&(k==-1||dis[i]<dis[k]))k=i;}if(k==-1)break;vis[k]=1;for(i=1;i<=n;i++)dis[i]=min(dis[i],dis[k]+map[k][i]);}}int main(){while(scanf("%d%d%d",&n,&m,&s)!=EOF){int a,b,c,i,j;int n1,a1,b1;int mi;/*for(i=1;i<=n;i++)//两种初始化方式都行 for(j=1;j<=n;j++){if(i==j)map[i][j]=0;elsemap[i][j]=mx;}*/memset(map,mx,sizeof(map));while(m--){scanf("%d%d%d",&a,&b,&c);if(map[b][a]>c) //重点,只能从b到a单项赋值,因为车是单向走的,并且是反向建图 map[b][a]=c;}scanf("%d",&n1);mi=mx;djs(s);while(n1--){scanf("%d",&a1);if(dis[a1]<mi)mi=dis[a1];}if(mi==mx)printf("-1\n");elseprintf("%d\n",mi);}return 0;}<pre class="cpp" name="code">//SPFA解 #include<stdio.h>#include<string.h>#include<queue>#define INF 0x3f3f3f3f#include<algorithm>using namespace std;int dis[1100],vis[1100];struct Edge{int from,to,val,next;}edge[20100];int head[20100],edgenum;void add(int u,int v,int w){Edge E={u,v,w,head[u]};edge[edgenum]=E;head[u]=edgenum++;}void SPFA(int x){queue<int>q;memset(vis,0,sizeof(vis));memset(dis,INF,sizeof(dis));q.push(x);dis[x]=0;vis[x]=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].to;if(dis[v]>dis[u]+edge[i].val){dis[v]=dis[u]+edge[i].val;if(!vis[v]){vis[v]=1;q.push(v);}}}}}int n,m,s;int main(){while(scanf("%d%d%d",&n,&m,&s)!=EOF){int a,b,c,n1,a1,mm;memset(head,-1,sizeof(head));edgenum=0;while(m--){scanf("%d%d%d",&a,&b,&c);add(b,a,c);}mm=INF;scanf("%d",&n1);SPFA(s);while(n1--){scanf("%d",&a1);mm=min(mm,dis[a1]);}if(mm==INF)printf("-1\n");elseprintf("%d\n",mm);}return 0;}
0 0
- Choose the best route 2680 (dijkstra,反向建图)
- hdu 2680 Choose the best route (Dijkstra & 反向图)
- HDOJ Choose the best route 2680【最短路Dijkstra+反向建图】
- HDU 2680 Choose the best route 【最短路 反向建图 dijkstra & SPFA 】
- hdu 2680 Choose the best route【dijstra+反向建图】
- HDOJ 2680 Choose the best route(最短路—反向图,dijkstra算法)
- HDU 2680 Choose the best route(Dijkstra,建图的方式很巧妙)
- HDU 2680 Choose the best route(dijkstra)
- HDU--2680Choose the best route【Dijkstra】
- HDOJ-2680Choose the best route(Dijkstra)
- hdu 2680 Choose the best route (dijkstra)
- 2680 Choose the best route【dijkstra】
- hdoj 2680 Choose the best route 【dijkstra】
- HDU 2680 Choose the best route dijkstra
- HDU 2680-Choose the best route(Dijkstra)
- hdu2680 Choose the best route (dijkstra)
- HDOJ 2680 Choose the best route(最短路,dijkstra)
- HDU-2680 Choose the best route(最短路[Dijkstra])
- 使用Atomikos Transactions Essentials实现多数据源JTA分布式事务
- CTS Verifier
- 详解coredump
- 关于LoginActivity的总结
- PowerPC PPC460-S MMU(二)
- Choose the best route 2680 (dijkstra,反向建图)
- 百度笔试题:求序列里最长的非降序列
- static关键字的用法(C++)
- CentOS系统下的Hadoop集群(第7期)_Eclipse开发环境设置
- 扫描二维码下载
- Tab控件子主对话框发送消息
- 散列函数的构造方法
- bootstrap布局 网格系统
- UIWebView与js交互(二)