Question:
It takes the shortest time to transmit information from a vertex to another vertex. Number of the person who receives the information at the latest.
Algorithm:
1. the Dijkstra algorithm can only find the single-source shortest path at a time, while the Floyd algorithm can find the shortest path for each pair of vertices.
2. enumerate the maximum straight from each point to each point, and compare the maximum straight of each point to obtain the answer. The maximum direct time is the time required to receive the information at the latest.
View code
#include<stdlib.h>#include<string.h>#include<stdio.h>int mp[210][210];int N;const int inf = 0x7f7f7f7f;void Floyd( ){ for( int k = 1; k <= N; k++) for( int i = 1; i <= N; i++) for( int j = 1; j <= N; j++) { if( mp[k][j] != inf && mp[i][j] > mp[i][k] + mp[k][j] && mp[k][j] != inf ) mp[i][j] = mp[i][k] + mp[k][j]; }}void solve( ){ int maxn = inf, ans = 0; int minx = 0; for( int i = 1; i <= N; i++) { minx = 0; for( int j = 1; j <= N; j++) { if( mp[i][j] > minx ) minx = mp[i][j]; } if( minx < maxn ) { maxn = minx; ans = i; } } if( ans == 0 ) puts("disjoint"); else printf("%d %d\n", ans, maxn);}int main( ){ int M, a, b; while( scanf("%d",&N), N ) { for( int i = 1; i <= N; i++) for( int j = 1; j <= N; j++) mp[i][j] = ( i == j ) ? 0 : inf; for( int i = 1; i <= N; i++) { scanf("%d", &M); for( int j = 1; j <= M; j++) { scanf("%d%d", &a, &b); mp[i][a] = b; } } Floyd(); solve( ); } return 0;}