HDU 2112 HDU Today(最短路 SPFA Floyd-Warshall算法)

来源:互联网 发布:ubuntu 15.04 下载 编辑:程序博客网 时间:2024/05/17 07:32

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2112

HDU Today

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 19438    Accepted Submission(s): 4565


Problem Description
经过锦囊相助,海东集团终于度过了危机,从此,HDU的发展就一直顺风顺水,到了2050年,集团已经相当规模了,据说进入了钱江肉丝经济开发区500强。这时候,XHD夫妇也退居了二线,并在风景秀美的诸暨市浬浦镇陶姚村买了个房子,开始安度晚年了。
这样住了一段时间,徐总对当地的交通还是不太了解。有时很郁闷,想去一个地方又不知道应该乘什么公交车,在什么地方转车,在什么地方下车(其实徐总自己有车,却一定要与民同乐,这就是徐总的性格)。
徐总经常会问蹩脚的英文问路:“Can you help me?”。看着他那迷茫而又无助的眼神,热心的你能帮帮他吗?
请帮助他用最短的时间到达目的地(假设每一路公交车都只在起点站和终点站停,而且随时都会开)。
 

Input
输入数据有多组,每组的第一行是公交车的总数N(0<=N<=10000);
第二行有徐总的所在地start,他的目的地end;
接着有n行,每行有站名s,站名e,以及从s到e的时间整数t(0<t<100)(每个地名是一个长度不超过30的字符串)。
note:一组数据中地名数不会超过150个。
如果N==-1,表示输入结束。
 

Output
如果徐总能到达目的地,输出最短的时间;否则,输出“-1”。
 

Sample Input
6xiasha westlakexiasha station 60xiasha ShoppingCenterofHangZhou 30station westlake 20ShoppingCenterofHangZhou supermarket 10xiasha supermarket 50supermarket westlake 10-1
 

Sample Output
50Hint:The best route is:xiasha->ShoppingCenterofHangZhou->supermarket->westlake


真是醉了的题目,错了n次,有题意理解错误,数组开小错误等等,,不忍直视了

贴一个用map做的吧

【源代码】

#include <cstdio>#include <queue>#include <string>#include <cstring>#include <iostream>#include <vector>#include <map>using namespace std;const int maxn = 155;const int maxe = 155;const int INF = 0xfffffff;int n,cnt;int minDist[maxn];bool inqueue[maxn];int head[maxn];int edgeNum=0;struct node{int to,len,next; node(int to=0,int len=0):to(to),len(len){}}edge[20005]; //开10010就re了 void addEdge(int a,int b,int len){edge[edgeNum].to=b;edge[edgeNum].len=len;edge[edgeNum].next=head[a];head[a]=edgeNum++;}void init(){memset(head,-1,sizeof(head));edgeNum=0;}int SPFA(){for(int i=1;i<=cnt;i++){minDist[i]=INF;inqueue[i]=0;}queue<int>Q;inqueue[1]=1;Q.push(1);minDist[1]=0;while(!Q.empty()){int v1 =Q.front();Q.pop();inqueue[v1]=0;for(int i=head[v1];i!=-1;i=edge[i].next){int v2=edge[i].to;int len=edge[i].len;if(minDist[v2]>minDist[v1]+len){minDist[v2]=minDist[v1]+len;if(!inqueue[v2]){inqueue[v2]=1;Q.push(v2);}}}}if(minDist[2]>=INF) return -1;return minDist[2];}int main(){map<string,int>mp; //遇到字符串根int匹配的题,果断用map!!char st[35],end[35]; char tmp1[35],tmp2[35]; int len;while(scanf("%d",&n)!=EOF){if(n==-1) break;init();mp.clear(); //不要忘了清空cnt=0;scanf("%s %s",st,end);mp[st]=++cnt;if(mp[end]==0){ //开头结尾不一样时mp[end]=++cnt;}for(int i=0;i<n;i++){scanf("%s%s%d",tmp1,tmp2,&len);if(mp[tmp1]==0) mp[tmp1]=++cnt;if(mp[tmp2]==0) mp[tmp2]=++cnt;addEdge(mp[tmp1],mp[tmp2],len);addEdge(mp[tmp2],mp[tmp1],len);}if(mp[st]==mp[end]){printf("0\n");continue;}int ans=SPFA();printf("%d\n",ans);}return 0;}

【Floyd-Warshall】

#include <iostream>#include <cstdio>#include <map>#include <string>using namespace std;int n,cnt;const int maxn = 155;const int INF = 0xfffffff;int Map[155][155];void init(){for(int i=0;i<maxn;i++){for(int j=0;j<maxn;j++){if(i==j)Map[i][j]=0;else{Map[i][j]=INF;}}}}int main(){map<string,int>mp;string st,end,tmp1,tmp2;int len;while(scanf("%d",&n)!=EOF&&n!=-1){mp.clear();init();cnt=0;cin>>st>>end;mp[st]=++cnt;if(st!=end)mp[end]=++cnt;for(int i=0;i<n;i++){cin>>tmp1>>tmp2>>len;if(mp[tmp1]==0)mp[tmp1]=++cnt;if(mp[tmp2]==0)mp[tmp2]=++cnt;if(Map[mp[tmp1]][mp[tmp2]]>len){ //判断重边Map[mp[tmp1]][mp[tmp2]]=len;Map[mp[tmp2]][mp[tmp1]]=len;}}for(int k=1;k<=cnt;k++){for(int i=1;i<=cnt;i++){for(int j=1;j<=cnt;j++){if(Map[i][j]>Map[i][k]+Map[k][j])Map[i][j]=Map[i][k]+Map[k][j];}}}if(st==end)printf("0\n");else if(Map[1][2]>=INF)printf("-1\n");else printf("%d\n",Map[1][2]);}return 0;}


0 0