POJ Stockbroker Grapevine(floyd)

https://vjudge.net/problem/POJ-1125

题意:

题意不是很好理解,首先输入一个n,表示有n个股票经纪人,接下来输入n行,每行第一个数m为该股票经纪人认识的经纪人数,然后输入m对数(a,t),表示第i个经纪人把消息传给第a个经纪人所需要的时间。

计算将消息传遍所有人所需要的最少时间。

思路:

起点任意,用floyd比较好。因为floyd求出的是每两点之间的最短路,所以最后计算最小时间时,需要先取最大值,比如说1号经纪人为起点,因为谁都可能为终点,所以枚举所有人,将最大时间作为最小时间,这个应该不难理解。

 1 #include<iostream>
 2 #include<algorithm>
 3 #include<string>
 4 #include<cstring>
 5 using namespace std;
 6
 7 const int maxn = 100 + 5;
 8 const int INF = 0x3f3f3f3f;
 9
10 int n;
11 int d[maxn][maxn];
12
13
14 void floyd()
15 {
16     for (int k = 1; k <= n;k++)
17     for (int i = 1; i <= n;i++)
18     for (int j = 1; j <= n; j++)
19         d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
20 }
21
22 int main()
23 {
24     //freopen("D:\\txt.txt", "r", stdin);
25     int x, a, t;
26     while (~scanf("%d", &n) && n)
27     {
28         for (int i = 1; i <= n; i++)
29         {
30             d[i][i] = 0;
31             for (int j = 1; j < i; j++)
32                 d[i][j] = d[j][i] = INF;
33         }
34
35         for (int i = 1; i <= n; i++)
36         {
37             scanf("%d", &x);
38             while (x--)
39             {
40                 scanf("%d%d", &a, &t);
41                 d[i][a] = t;
42             }
43         }
44
45         floyd();
46         int stock;
47         int ans = INF;
48         for (int i = 1; i <= n; i++)
49         {
50             int MAX = 0;
51             for (int j = 1; j <= n; j++)
52             {
53                 if (i == j) continue;
54                 MAX = max(MAX, d[i][j]);
55             }
56             if (ans > MAX)
57             {
58                 ans = MAX;
59                 stock = i;
60             }
61         }
62         if (ans == INF)
63             printf("disjoint\n");
64         else
65             printf("%d %d\n", stock, ans);
66     }
67     return 0;
68 }
时间: 2024-11-09 03:15:57

POJ Stockbroker Grapevine(floyd)的相关文章

poj 2240 Arbitrage (Floyd)

链接:poj 2240 题意:首先给出N中货币,然后给出了这N种货币之间的兑换的兑换率. 如 USDollar 0.5 BritishPound 表示 :1 USDollar兑换成0.5 BritishPound. 问在这N种货币中是否存在货币经过若干次兑换后,兑换成原来的货币能够使货币量添加. 思路:本题事实上是Floyd的变形.将变换率作为构成图的路径的权值.只是构成的图是一个有向图. 最后将松弛操作变换为:if(dis[i][j]<dis[i][k]*dis[k][j]). #includ

poj 1125 Stockbroker Grapevine(多源最短路)

链接:poj 1125 题意:输入n个经纪人,以及他们之间传播谣言所需的时间, 问从哪个人开始传播使得所有人知道所需时间最少,这个最少时间是多少 分析:因为谣言传播是同时的,对于某条路径使得所有人都知道的时间,不是时间的总和,而是路径中最长的边 从多条路径的最长边,找出最小值,因为为多源最短路,用Floyd比较方便 #include<stdio.h> #include<limits.h> int a[105][105]; void floyd(int n) { int i,j,k,

POJ 3414 Pots(罐子)

p.MsoNormal { margin-bottom: 10.0000pt; font-family: Tahoma; font-size: 11.0000pt } h1 { margin-top: 5.0000pt; margin-bottom: 5.0000pt; text-align: left; font-family: 宋体; font-weight: bold; font-size: 24.0000pt } span.10 { font-family: "Times New Rom

poj 3399 Product(模拟)

# include <stdio.h> # include <string.h> # include <algorithm> using namespace std; int cmp(int x,int y) { return x>y; } int main() { int a[110],a1[110],a2[110],ans[110]; int n,k,k1,k2,i,k3; while(~scanf("%d%d",&n,&k

POJ 1840 Eqs(暴力)

Description Consider equations having the following form: a1x13+ a2x23+ a3x33+ a4x43+ a5x53=0 The coefficients are given integers from the interval [-50,50]. It is consider a solution a system (x1, x2, x3, x4, x5) that verifies the equation, xi∈[-50,

POJ 3034 Whac-a-Mole(DP)

题目链接 题意 : 在一个二维直角坐标系中,有n×n个洞,每个洞的坐标为(x,y), 0 ≤ x, y < n,给你一把锤子可以打到地鼠,最开始的时候,你可以把锤子放在任何地方,如果你上一秒在(x1,y1),那下一秒直线移动到的整数点(x2,y2)与这个点的距离小于等于d,并且当锤子移动(x2,y2)这个点时,所有在两点的直线上的整点数都可以打到.例如(0,0)移动到(0,3).如果(0,1),(0,2)有老鼠出现就会被打到.求能够打的最多老鼠. 思路 : Dp[i][j][k]代表点(i,j)

poj 2309 BST(数学题)

BST Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8440   Accepted: 5093 Description Consider an infinite full binary search tree (see the figure below), the numbers in the nodes are 1, 2, 3, .... In a subtree whose root node is X, we c

poj 2485 Highways(最小生成树)

题目链接:http://poj.org/problem?id=2485 Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the traffic is difficult in Flatopia. The Flatopian government is aware of this problem. They're plann

poj 1751 Highways (prim )

Highways Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9080   Accepted: 2536   Special Judge Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has a very poor system of public highways. The Flatopian