Question:
Spread rumors between stock brokers. Give a contact network for stock brokers: w when a is connected to B.
Q: which one can be used as the source of the rumor to make rumor spread to everyone the fastest, and the time it takes to reach everyone.
Ideas:
The Floyd algorithm can be used to find the shortest path of the entire source. As long as every source s is enumerated, the maximum time t from s to others is recorded, and t is compared.
# Include <cstdio> # include <cstring> using namespace std; const int INF = 0x3f3f3f; const int MAXN = 105; int adj [MAXN] [MAXN]; int n; void Floyd () {for (int k = 1; k <= n; k ++) for (int I = 1; I <= n; I ++) for (int j = 1; j <= n; j ++) if (adj [I] [j]> adj [I] [k] + adj [k] [j]) adj [I] [j] = adj [I] [k] + adj [k] [j];} int main () {while (scanf ("% d ", & n), n) {memset (adj, 0x3f, sizeof (adj); for (int I = 1, m; I <= n; I ++) {adj [I] [I] = 0; // enter 0 scanf ("% d", & m); for (int k = 1, j, w; k <= m; k ++) {scanf ("% d", & j, & w); adj [I] [j] = w ;}} floyd (); int s = 0, cost = INF; int tot; for (int I = 1; I <= n; I ++) {tot = 0; for (int j = 1; j <= n; j ++) {if (tot <adj [I] [j]) tot = adj [I] [j];} if (tot <cost) {cost = tot; s = I ;}} if (cost = INF) printf ("disjoint \ n "); else printf ("% d \ n", s, cost );}}