This question is the maximum Spanning Tree and the minimum weight edge on the bottleneck.
The Code is as follows:
#include <cstdio>#include <cstring>#include <algorithm>#include <string>#include <map>#include <iostream>using namespace std;const int N = 210;const int INF = -1;const int INFB = 100000;int n, r, g[N][N], p[N], f[N][N], d[N];bool mark[N];string S, T;void prim() { bool vis[N]; memset(vis, 0, sizeof(vis)); for ( int i = 1; i <= n; ++i ) d[i] = INF, p[i] = INF; d[1] = 0; int mi, v, ans = 0; for ( int u = 0; u < n; ++u ) { mi = INF; for ( int i = 1; i <= n; ++i ) if ( !vis[i] && d[i] > mi ) v = i, mi = d[i]; ans += mi; //cout << mi << endl; vis[v] = true; for ( int i = 1; i <= n; ++i ) if ( !vis[i] && d[i] < g[v][i] ) p[i] = v, d[i] = g[v][i]; } //cout << ans << endl;}void dfs( int v ) { for ( int u = 1; u <= n; ++u ) if ( !mark[u] && p[u] == v ) { mark[u] = true; //cout << u << ' '; for ( int x = 1; x <= n; ++x ) if ( mark[x] && x != u ){ // cout << f[x][v] << ' ' << g[u][v] << endl; f[x][u] = f[u][x] = min( f[x][v], g[u][v] ); } dfs(u); }} int main(){ int icase = 1; while ( scanf("%d%d", &n, &r), !(!n&&!r) ) { getchar(); map <string,int> mp; int id = 1; for ( int i = 1; i <= n; ++i ) for ( int j = 1; j <= n; ++j ) g[i][j] = INF; mp.clear(); while ( r-- ) { string s1, s2; int w; cin >> s1 >> s2 >> w; if ( !mp[s1] ) mp[s1] = id++; if ( !mp[s2] ) mp[s2] = id++; int u = mp[s1], v = mp[s2]; g[u][v] = g[v][u] = w; } cin >> S >> T; prim(); for ( int i = 1; i <= n; ++i ) for ( int j = i; j <= n; ++j ) f[i][j] = f[j][i] = INFB; memset( mark, 0, sizeof(mark)); mark[1] = true; dfs( 1 ); //for ( int i = 1; i <= n; printf("\n"),++i ) for ( int j = 1; j <= n; ++j ) cout << f[i][j] << ' '; int u = mp[S], v = mp[T]; printf("Scenario #%d\n%d tons\n\n", icase++, f[u][v]); }}