6 xiasha westlake xiasha station 60 xiasha ShoppingCenterofHangZhou 30 station westlake 20 ShoppingCenterofHangZhou supermarket 10 xiasha supermarket 50 supermarket westlake 10 -1
50 Hint: The best route is: xiasha->ShoppingCenterofHangZhou->supermarket->westlake 虽然偶尔会迷路,但是因为有了你的帮助 **和**从此还是过上了幸福的生活。 ――全剧终――
基本上是floyd-warshall的模板题,直接调用函数,注意map的使用即可,还要特别判断n=0的情况。
#include<map> #include<stack> #include<queue> #include<cmath> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #pragma commment(linker,"/STACK: 102400000 102400000") #define mset0(t) memset(t,0,sizeof(t)) #define lson a,b,l,mid,cur<<1 #define rson a,b,mid+1,r,cur<<1|1 using namespace std; const double eps=1e-5; const int MAXN=165; const int INF=0x3f3f3f3f; int d[MAXN][MAXN],prev[MAXN],n,v; void warshal_floyd() { for(int k=1; k<=v; k++) for(int i=1; i<=v; i++) for(int j=1; j<=v; j++) d[i][j]=min(d[i][j],d[i][k]+d[k][j]); } int main() { while(scanf("%d",&n)&&n!=-1) { map<string,int>m; for(int i=1; i<=165; i++) for(int j=i; j<=165; j++) { if(i==j) d[i][j]=0; else d[i][j]=d[j][i]=INF; } string s,e,a,b; int cnt=0,t; cin>>s>>e; if(n==0)//特判有0条路的情况 { if(s!=e) printf("-1\n"); else printf("0\n"); continue; } m[s]=++cnt; if(m[e]==0) m[e]=++cnt; for(int i=0; i<n; i++) { cin>>a>>b>>t; if(m[a]==0) m[a]=++cnt; if(m[b]==0) m[b]=++cnt; d[m[a]][m[b]]=d[m[b]][m[a]]=t; } v=cnt; warshal_floyd(); printf("%d\n",d[m[s]][m[e]]==INF?-1:d[m[s]][m[e]]); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
HDU 2112 HDU Today(floyd-warshall+map)
原文地址:http://blog.csdn.net/noooooorth/article/details/47051075