vijosP1046 观光旅游
【思路】
Floyd求解最小环。
【代码】
1 #include<iostream> 2 using namespace std; 3 4 const int maxn = 100+10; 5 const int INF=1e8; 6 int f[maxn][maxn],dist[maxn][maxn]; 7 int n,m,min_loop; 8 9 void Floyd() 10 { 11 min_loop=INF; 12 for(int k=1;k<=n;k++) 13 { 14 for(int i=1;i<k;i++) 15 for(int j=i+1;j<k;j++) 16 if(f[i][j]+dist[i][k]+dist[k][j]<min_loop) 17 min_loop=f[i][j]+dist[i][k]+dist[k][j]; 18 19 for(int i=1;i<=n;i++) 20 for(int j=1;j<=n;j++) 21 if(f[i][k]<INF && f[k][j]<INF) 22 f[i][j]=min(f[i][j],f[i][k]+f[k][j]); 23 } 24 } 25 26 int main() { 27 ios::sync_with_stdio(false); 28 while(cin>>n>>m) 29 { 30 for(int i=1;i<=n;i++) 31 { 32 f[i][i]=dist[i][i]=0; 33 for(int j=i+1;j<=n;j++) 34 f[i][j]=f[j][i]=dist[i][j]=dist[j][i]=INF; 35 } 36 int u,v,w; 37 for(int i=0;i<m;i++) { 38 cin>>u>>v>>w; 39 dist[u][v]=dist[v][u]=f[u][v]=f[v][u]=w; 40 } 41 Floyd(); 42 if(min_loop==INF) cout<<"No solution.\n"; 43 else cout<<min_loop<<"\n"; 44 } 45 return 0; 46 }