hdoj 2680 Choose the best route(dijkstra 多起点 单终点)
2015-08-18 17:15
225 查看
hdoj 2680 http://acm.hdu.edu.cn/showproblem.php?pid=2680题目大意:一个小孩想要去看望他的朋友,他家附近有好几个出发站,去朋友家需要倒不同的车,题中给了多条车站之间的铁路距离,你需要选择一个出发站,找到去朋友家的最短路径,输出路径长度。 心得:一开始自己的想法就是输入多个起点调用多次dijksta,可是一直超时,但该优化的都优化了,然后从大神那学来一招,反向建图!因为本题起点较多,而终点只有一个,如果反过来,把终点当成起点,那么不管终点有多少,只需调用一次dijkstra就ok,然后再把道路连接反过来(即把map[a]写成map[b][a]),还要特别注意此题为[b]有向图,建图时必须单向赋值,反向为无穷大!
Choose the best route
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 10527 Accepted Submission(s): 3386Problem DescriptionOne 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. InputThere 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. OutputThe 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 Input5 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 1Sample Output
1 -1ac码
<pre name="code" class="cpp">#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;int vis[20010];int d[20010];int cost[1010][1010];int n,m,start,e,a,b,c;int inf=0x3f3f3ff;void dijkstra(){ memset(vis,0,sizeof(vis)); int v,i,j; for(v=1;v<=n;v++) d[v]=inf; d[start]=0; for(int f=1;f<=n;f++) { v=-1; for(i=1;i<=n;i++) { if(!vis[i]&&(v==-1||d[i]<d[v])) { v=i; } } //if(v==-1)break; vis[v]=1; for(j=1;j<=n;j++) d[j]=min(d[j],d[v]+cost[v][j]); }}int main(){ int i,j,e,r; while(scanf("%d%d%d",&n,&m,&start)!=EOF) //把终点赋值成起点 { for(i=1;i<=n;i++) for(j=i+1;j<=n;j++) cost[i][j]=cost[j][i]=inf; for(i=1;i<=m;i++) { scanf("%d%d%d",&a,&b,&c); cost[b][a]=(c>cost[b][a]?cost[b][a]:c); //如果两个站点出现多条路,选最短的 } scanf("%d",&r); int min=inf; dijkstra(); for(i=1;i<=r;i++) { scanf("%d",&e); if(min>d[e]) min=d[e]; //不断让最短路径赋值给min } if(min==inf) printf("-1\n"); else printf("%d\n",min); } return 0;}
相关文章推荐
- 初始UI(UILabel、UIButton、UITextField)
- Java finalize方法使用
- 统计大写字母个数
- java 连接oracle数据库
- 【转载】HBase 数据库检索性能优化策略
- Java 发送邮件
- JSTL中的fmt
- 手动编译安装lamp 遇到的问题与解决方法
- Nginx配置文件详细说明
- IOS开发第三方语音-微信语音
- assert函数用法总结
- hdoj 1873 看病要排队
- js笔试题
- 即时通讯研究学习
- eclipse & as 项目编码修改
- gdb: info breakpoints显示所有断点
- SVN不能提交,让更新,更新完还是不能提交的解决办法
- python list遍历时删除元素的推荐做法
- 我的日记
- 预处理