标签:
题目链接:
Time Limit: 15000/5000 MS (Java/Others)
Memory Limit: 32768/32768 K (Java/Others)
#include <bits/stdc++.h> using namespace std; typedef long long LL; const LL mod=1e9+7; const int N=1e5+6; const int inf=0x3f3f3f3f; int n,cnt,num; map<string,int>mp; struct Edge { int to,next,val; }; Edge edge[N]; int dis[200],p[200][200],flag[200]; int dijkstra() { memset(flag,0,sizeof(flag)); dis[1]=0; for(int i = 2;i < num;i ++) { dis[i]=p[1][i]; } flag[1]=1; int pre; for(int j = 1;j < num;j ++) { int mmin = inf; for(int i = 1;i < num;i ++) { if(flag[i] == 0 &&dis[i] < mmin) { mmin=dis[i]; pre=i; } } if(mmin == inf)break; flag[pre] = 1; for(int i =1;i < num;i++) { if(flag[i] == 0 &&dis[i]>dis[pre]+p[pre][i]) { dis[i]=dis[pre]+p[pre][i]; } } } } int main() { while(1) { scanf("%d",&n); if(n==-1)break; for(int i=1;i<200;i++) { for(int j=1;j<200;j++) { if(i==j)p[i][j]=0; else p[i][j]=inf; } } memset(dis,inf,sizeof(dis)); string str1,str2; int v; mp.clear(); cin>>str1>>str2; int x=0; if(str1==str2)x=1; else {mp[str1]=1; mp[str2]=2;} num=3; for(int i=0;i < n;i++) { cin>>str1>>str2>>v; if(!mp[str1])mp[str1]=num++; if(!mp[str2])mp[str2] = num++; int pos1=mp[str1],pos2=mp[str2]; p[pos2][pos1]=p[pos1][pos2]=min(p[pos1][pos2],v); } if(x==0){ dijkstra(); if(dis[2]==inf)printf("-1\n"); else printf("%d\n",dis[2]); } else printf("0\n"); } return 0; }
标签:
原文地址:http://www.cnblogs.com/zhangchengc919/p/5444841.html