标签:des style blog color os io 数据 for
#include<stdio.h> #include<iostream> #include<map> #include<queue> using namespace std; #define INF 99999999 int Map[155][155],n,node[155]; void init() { for(int i=0;i<=150;i++) for(int j=i;j<=150;j++) Map[i][j]=Map[j][i]=node[i]=INF; } void spfa(int star) { int inQ[155]={0}; queue<int>Q; inQ[star]=1; Q.push(star); node[star]=0; while(!Q.empty()) { star=Q.front(); Q.pop(); inQ[star]=0; for(int i=1;i<=n;i++) if(Map[star][i]+node[star]<node[i]) { node[i]=Map[star][i]+node[star]; if(!inQ[i]) Q.push(i),inQ[i]=1; } } } int main() { int star,end,m,a,b,p; char aa[35],bb[35]; map<string,int>loc; while(scanf("%d",&m)>0&&m!=-1) { init(); n=1; loc.clear(); scanf("%s%s",aa,bb); loc[aa]=n; star=n; if(!loc[bb]){n++;loc[bb]=n;} end=loc[bb]; while(m--) { scanf("%s%s%d",aa,bb,&p); if(!loc[aa]){n++;loc[aa]=n;}a=loc[aa]; if(!loc[bb]){n++;loc[bb]=n;}b=loc[bb]; if(Map[a][b]>p) Map[a][b]=Map[b][a]=p; } if(star==end) { printf("0\n");continue; } spfa(star); if(node[end]==INF) node[end]=-1; printf("%d\n",node[end]); } }
hdu2112(HDU Today 简单最短路),布布扣,bubuko.com
标签:des style blog color os io 数据 for
原文地址:http://www.cnblogs.com/mis-xiao/p/3921401.html