Poj 1125 stockbroker grapevine

Source: Internet
Author: User

Question:To give you n stock brokers, you need to find the shortest time to contact everyone. If yes, output a string. If no, output a string.

Next there are N groups of data. Each group of data starts with M contacts for the I broker, next, M indicates the first contact of the data. The second number indicates the time required to contact the contact.

Ideas:The main purpose of this question is to understand the meaning of the question. After understanding it, it will be very simple,You only need to first find the shortest distance of each two points, and then find the shortest distance required to reach the J point using the I vertex as the vertex, you can contact all the people. Because we can find the shortest distance of every two points, we can use the Floyd algorithm, which is the easiest to understand!

So the AC code:

# Include <cstdio> # include <cstring> # include <algorithm> using namespace STD; # define INF 100int d [120] [120]; int main () {int I, j, N, K; while (scanf ("% d", & N )! = EOF) {If (n = 0) break; for (I = 1; I <= N; I ++) for (j = 1; j <= N; j ++) {d [I] [J] = inf ;}for (I = 1; I <= N; I ++) {int U; scanf ("% d", & U); For (j = 1; j <= u; j ++) {int V, W; scanf ("% d", & V, & W); D [I] [v] = W ;}} for (I = 1; I <= N; I ++) // obtain the shortest distance for (j = 1; j <= N; j ++) for (k = 1; k <= N; k ++) {If (J! = K & D [J] [k]> d [J] [I] + d [I] [k]) d [J] [k] = d [J] [I] + d [I] [k];} int temp, Maxx, Minn = inf; for (I = 1; I <= N; I ++) // The vertex with I as the graph every time {Maxx = 0; For (j = 1; j <= N; j ++) // find the maximum distance from vertex I if (I! = J & Maxx <D [I] [J]) Maxx = d [I] [J]; If (Maxx <Minn) // replace {Minn = Maxx; temp = I ;}} if (Minn <inf) printf ("% d \ n", temp, Minn); else printf ("disjoint \ n ");} return 0 ;}


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.