Topic Introduction: A undirected forward weight graph is used to find the shortest path length between any two nodes. To solve this problem directly, replace the DP sub of Floyd with G [I] [J] = min (G [I] [J], max (G [I] [K], G [k] [J]), and bidirectional assignment code for undirected graphs
#include <bits/stdc++.h>using namespace std;int G[110][110];int main(){ int n,m,t,cnt=0; while(cin>>n>>m>>t&&m) { for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) G[i][j]=i==j?0:INT_MAX; while(m--) { int t1,t2,t3; cin>>t1>>t2>>t3; G[t1][t2]=t3,G[t2][t1]=t3; } for(int k=1;k<=n;k++) for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) if(i!=j) G[i][j] = min(G[i][j], max(G[i][k], G[k][j])); if(cnt) cout<<endl; printf("Case #%d\n",++cnt); while(t--) { int t1,t2; cin>>t1>>t2; if(G[t1][t2]<INT_MAX) cout<<G[t1][t2]; else cout<<"no path"; cout<<endl; } }}
Ultraviolet A 10048 audiophobia ultraviolet A-10048