floyd之后 枚举找最小就行 会有重边
View Code
1 /* 2 ID: shangca2 3 LANG: C++ 4 TASK: comehome 5 */ 6 #include <iostream> 7 #include<cstdio> 8 #include<cstring> 9 #include<algorithm> 10 #include<stdlib.h> 11 #define INF 0x3f3f3f 12 using namespace std; 13 int w[110][110]; 14 int main() 15 { 16 freopen("comehome.in","r",stdin); 17 freopen("comehome.out","w",stdout); 18 int i,j,k,n,d,a,b,m=52; 19 char c1,c2,c; 20 cin>>n; 21 for(i = 0 ; i < m ;i++) 22 for(j = 0 ; j < m ; j++) 23 w[i][j] = INF; 24 for(i = 0 ; i < n ; i++) 25 { 26 cin>>c1>>c2>>d; 27 if(c1>='A'&&c1<='Z') 28 a = c1-'A'; 29 else 30 a = c1-'a'+26; 31 if(c2>='A'&&c2<='Z') 32 b = c2-'A'; 33 else 34 b = c2-'a'+26; 35 if(w[a][b]>d) 36 { 37 w[a][b] = d; 38 w[b][a] = d; 39 } 40 } 41 for(i = 0 ; i < m ; i++) 42 w[i][i] = 0; 43 for(i = 0 ; i < m ; i++) 44 for(j = 0 ; j < m ; j++) 45 for(k = 0 ; k < m ; k++) 46 if(w[j][k]>w[j][i]+w[i][k]) 47 w[j][k] = w[j][i]+w[i][k]; 48 int ans = INF; 49 for(i = 0 ; i < 25 ; i++) 50 { 51 if(w[i][25]<ans) 52 { 53 c = i+'A'; 54 ans = w[i][25]; 55 } 56 } 57 cout<<c<<" "<<ans<<endl; 58 return 0; 59 }