Poj1125 (stockbroker grapevine)

Source: Internet
Author: User

Question:

The question will be given to you by N brokers. The example shows the time required for communication and dissemination between the first brokers and other brokers, ask which broker to start spreading the rumors as quickly as possible and calculate the time required. The time here is the least time spent in a shortest path. If an economic person does not output "disjoint" in the shortest path, (Data Shui)

 

Solution:

The shortest path algorithm dijstra loops through every broker to find the answer that requires the least time path, that is, the question.

 

Code:

# Include <algorithm> # include <iostream> # include <sstream> # include <cstdlib> # include <cstring> # include <cstdio> # include <string> # include <bitset> # include <vector> # include <queue> # include <stack> # include <cmath> # include <list> // # include <map> # include <set> using namespace std; /*************************************** /# define ll long # define int64 _ int64 /*************************** * ***********/CONS T int inf = 0x7f7f7f7f; const double EPS = 1e-8; const double PIE = ACOs (-1.0); const int d1x [] = {0,-, 1 }; const int d1y [] = {-,}; const int d2x [] = {0,-, 1}; const int d2y [] =, -}; const int FX [] = {-1,-1,-,}; const int FY [] = {-, 1, -1, 0, 1 }; /*************************************** /void openfile () {freopen ("data. in "," rb ", stdin); freopen (" data. out "," WB ", stdout );}/**************** * ***** Gorgeous split line, the template section is *****************/INT map [500] [500]; int lowcost [500]; int vis [500]; int N; int CE; int maax; void dijstra (INT Sta) {int I, j, k = 0; int CNT = 0; int min; for (I = 1; I <= N; I ++) {lowcost [I] = map [sta] [I]; vis [I] = 0 ;} vis [sta] = 1; // lowcost [sta] = 0; for (I = 1; I <n; I ++) {min = inf; for (j = 1; j <= N; j ++) {If (! Vis [J] & lowcost [J] <min) {min = lowcost [J]; k = J ;}} if (k) CNT ++; if (CNT = N-1) if (Min <maax) {maax = min; Ce = sta;} vis [k] = 1; for (j = 1; j <= N; j ++) if (! Vis [J] & lowcost [J]> lowcost [k] + map [k] [J]) lowcost [J] = lowcost [k] + map [k] [J];} int main () {While (scanf ("% d", & N) & N) {int I, j; for (I = 0; I <500; I ++) for (j = 0; j <500; j ++) map [I] [J] = inf; int A; int U, cost; for (I = 1; I <= N; I ++) {scanf ("% d", & A); For (j = 0; j <A; j ++) {scanf ("% d", & U, & cost); map [I] [u] = Cost ;}} maax = inf; for (I = 1; I <= N; I ++) dijstra (I ); printf ("% d \ n", Ce, maax) ;}return 0 ;}// disjoint is not judged. You can add them by yourself.
View code

 

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.