[POJ1125 Stockbroker Grapevine]

[题目来源]:Southern African 2001

[关键字]:图论

[题目大意]:股票经纪人要在一群人中散布一个传言,传言只能在认识的人中传递,题目将给出人与人的关系(是否认识),以及传言在某两个认识的人中传递所需的时间,要求程序给出以哪个人为起点,可以在好事最短的情况下,让所有人收到消息。

//====================================================================================================

[分析]:先用foyd求出多源最短路,然后枚举每一个作为起点并判断此时所需的最短时间,最后取出最优值。注意如果有一个点到其他点的最小值的最大值(所用最短时间)是maxlongint,则说明此图不连通输出无解。

[代码]:

View Code
 1 {
2 PROB:POJ1125
3 DATE:2011\10\15
4 }
5 program Project1;
6 const
7 INF = 1684300900;
8 var
9 n: longint;
10 map: array[0..200,0..200] of longint;
11
12 procedure init;
13 var
14 i, j, t, x, d: longint;
15 begin
16 readln(n);
17 if n = 0 then halt;
18 fillchar(map,sizeof(map),100);
19 for i := 1 to n do
20 begin
21 read(t);
22 for j := 1 to t do
23 begin
24 read(x,d);
25 map[i,x] := d;
26 end;
27 end;
28 end;
29
30 procedure floyd;
31 var
32 k, i, j, max, ans, p: longint;
33 f: boolean;
34 begin
35 for k := 1 to n do
36 for i := 1 to n do
37 for j := 1 to n do
38 if (i <> j) and (map[i,k] <> INF) and (map[k,j] <> INF) then
39 if map[i,j] > map[i,k]+map[k,j] then map[i,j] := map[i,k]+map[k,j];
40 ans := maxlongint;
41 for i := 1 to n do
42 begin
43 max := 0;
44 for j := 1 to n do
45 if (max < map[i,j]) and (i <> j) then max := map[i,j];
46 if max < ans then
47 begin
48 ans := max;
49 p := i;
50 end;
51 end;
52 if ans = INF then writeln('disjoint') else writeln(p,'',ans);
53 end;
54
55 begin
56 while 1 = 1 do
57 begin
58 init;
59 floyd;
60 end;
61 end.



转载于:https://www.cnblogs.com/procedure2012/archive/2011/11/02/2232667.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值