标签:
题目链接 : http://acm.hdu.edu.cn/showproblem.php?pid=2112
Description
Input
Output
Sample Input
6 xiasha westlake xiasha station 60 xiasha ShoppingCenterofHangZhou 30 station westlake 20 ShoppingCenterofHangZhou supermarket 10 xiasha supermarket 50 supermarket westlake 10 -1
Sample Output
50 Hint: The best route is: xiasha->ShoppingCenterofHangZhou->supermarket->westlake 虽然偶尔会迷路,但是因为有了你的帮助 **和**从此还是过上了幸福的生活。 ――全剧终――
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<stack> #include<math.h> #include<queue> #include<map> using namespace std; #define INF 0x3f3f3f3f #define N 12342 ///map可以理解为一个一对一的key,value对,也叫做键值对 ///通常用于快速寻找一个key的对应的value是多少 ///map<key,value>mymap; mymap[key]=value; int n; int s[200][200]; int v[200],t[200]; void dij(int a,int b) { memset(v,0,sizeof(v)); for(int i=1;i<=b;i++) t[i] = (i==a?0:INF); for(int i=1;i<=b;i++) { int minn = INF,w; for(int j=1;j<=b;j++) if(!v[j]&&t[j]<minn) { w = j; minn = t[j]; } v[w] = 1; if(minn == INF) break; for(int j=1;j<=b;j++) { if(!v[j]&&t[w]+s[w][j]<t[j]) t[j] = t[w] + s[w][j]; } } } int main() { int flag,len; map<string,int> mymap;///将字符串化为整数存储,然后进行Dij char s1[100],s2[100]; while(scanf("%d",&n)) { if(n == -1) break; mymap.clear();///清空 memset(s,INF,sizeof(s)); flag = 0; scanf("%s%s",s1,s2); if(strcmp(s1,s2)==0) flag = 1; mymap[s1] = 1; mymap[s2] = 2; int cnt = 3; for(int i=0;i<n;i++) { scanf("%s%s%d",s1,s2,&len); if(mymap[s1] == 0) mymap[s1] = cnt++; if(mymap[s2] == 0) mymap[s2] = cnt++; s[mymap[s1]][mymap[s2]] = s[mymap[s2]][mymap[s1]] = min(len,s[mymap[s1]][mymap[s2]]); } if(flag) { printf("0\n"); continue; } dij(1,cnt); printf("%d\n",t[2]==INF?-1:t[2]); } return 0; }
标签:
原文地址:http://www.cnblogs.com/biu-biu-biu-/p/5798339.html