hdoj 2680 Choose the best route 【dijkstra】
2015-08-18 19:27
357 查看
<span style="font-family: 'Times New Roman', Times, serif;">Choose the best route</span>
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 61 Accepted Submission(s) : 31
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="" directed="" ways="" between="" .(maybe="" there="" are=""
several="" two="" .)="" s="" station="" that="" near="" kiki’s="" friend’s="" home.="" then="" follow="" lines="" ,each="" line="" contains="" three="" integers="" p="" ,="" q="" t="" (0<t<="1000)." means="" from="" to="" is="" a="" way="" it="" will="" costs=""
minutes="" .="" with="" an="" integer="" w(0<w<n),="" kiki="" can="" take="" at="" beginning.="" follows="" w="" these="" stations.="" <="" div="">
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
Author
dandelion
分析:
这道题用常规的dijkstra做不行,要改变一下,wa了我好几次。真是醉了。
代码:
#include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #define min(x,y) (x<y?x:y) using namespace std; const int INF=0x3f3f3f3f; int n,m,s; int map[1010][1010]; int dis[1010],vis[1010]; void dij() { memset(vis,0,sizeof(vis)); int i,j,k,mins; vis[0]=1; for(i=0;i<=n;i++) dis[i]=map[0][i]; for(i=0;i<=n;i++) { mins=INF; for(j=0;j<=n;j++) if(!vis[j]&&dis[j]<mins) mins=dis[k=j]; vis[k]=1; for(j=0;j<=n;j++) if(!vis[j]) dis[j]=min(dis[j],map[k][j]+dis[k]); } } int main() { while(scanf("%d%d%d",&n,&m,&s)!=EOF) { int a,b,c; memset(map,INF,sizeof(map)); while(m--) { scanf("%d%d%d",&a,&b,&c); if(map[a][b]>c) map[a][b]=c; } int t; scanf("%d",&t); while(t--) { int x; scanf("%d",&x); map[0][x]=0; } dij(); if(dis[s]==INF) printf("-1\n"); else printf("%d\n",dis[s]); } return 0; }
错误代码:
#include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #define min(x,y) (x<y?x:y) using namespace std; int n,m,s; const int INF=0x3f3f3f3f; int map[1010][1010]; int vis[1010], dis[1010]; void dij() { memset(dis,INF,sizeof(dis)); memset(vis,0,sizeof(vis)); dis[0]=0; while(true) { int v=-1; for(int i=0;i<=n;i++) if(!vis[i]&&(v==-1||dis[v]>dis[i])) v=i; if(v==-1) break; vis[v]=1; for(int j=0;j<=n;j++) dis[j]=min(dis[j],dis[v]+map[v][j]); } if(dis[s]==INF) printf("-1\n"); else printf("%d\n",dis[s]); } int main() { while(scanf("%d%d%d",&n,&m,&s)!=EOF) { int a,b,c,x,f; memset(map,INF,sizeof(map)); while(m--) { scanf("%d%d%d",&a,&b,&c); if(c<map[a][b]) map[a][b]=map[b][a]=c; } scanf("%d",&f); while(f--) { scanf("%d",&x); map[0][x]=0; } dij(); } return 0; }
相关文章推荐
- mac 下的 vi 指令
- Travelling Salesman Problem (hdu 5402 模拟)
- 引用
- 安装Apache的时候80端口被PID为4的系统进程所占用
- OC笔记NSDate
- 相同的 birthday
- 中文ubuntu里用户目录里的路径改成英文
- MySQL备份
- 设计模式交流有感
- 贪心算法专题小结——区间相关问题
- 解决Dialog 消失,输入法不消失的问题
- Activity生命周期
- IOS-TextField控件详解
- 将输入的字符串(字符串仅包含小写字母‘a’到‘z’),按照如下规则,循环转换后输出:a->b,b->c,…,y->z,z->a;若输入的字符串连续出现两个字母相同时,后一个字母需要连续转换2次。例如:
- 自定义异常类一
- fanqiang
- python视频教程大全集下载啦!超级全的教程!
- Dream------spark--spark集群的环境搭建
- Fragment 实现tab页卡切换并保存界面状态,动态添加Fragment
- jQuery 从input中读取的内容的类型