[Poj 1125] Stockbroker Grapevine [Floyd Shortest Path]

Source: Internet
Author: User

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 );}}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.