ACM PKU 1125 Stockbroker Grapevine 图论 Floyd算法

http://acm.pku.edu.cn/JudgeOnline/problem?id=1125 

Stockbroker Grapevine 

Time Limit:1000MS  Memory Limit:10000K 
Total Submit:2602 Accepted:1503 
Description Stockbrokers are known to overreact to rumours. You have been contracted to develop a method of spreading disinformation amongst the stockbrokers to give your employer the tactical edge in the stock market. For maximum effect, you have to spread the rumours in the fastest possible way. 

Unfortunately for you, stockbrokers only trust information coming from their "Trusted sources" This means you have to take into account the structure of their contacts when starting a rumour. It takes a certain amount of time for a specific stockbroker to pass the rumour on to each of his colleagues. Your task will be to write a program that tells you which stockbroker to choose as your starting point for the rumour, as well as the time it will take for the rumour to spread throughout the stockbroker community. This duration is measured as the time needed for the last person to receive the information. 
Input 
Your program will input data for different sets of stockbrokers. Each set starts with a line with the number of stockbrokers. Following this is a line for each stockbroker which contains the number of people who they have contact with, who these people are, and the time taken for them to pass the message to each person. The format of each stockbroker line is as follows: The line starts with the number of contacts (n), followed by n pairs of integers, one pair for each contact. Each pair lists first a number referring to the contact (e.g. a '1' means person number one in the set), followed by the time in minutes taken to pass a message to that person. There are no special punctuation symbols or spacing rules. 

Each person is numbered 1 through to the number of stockbrokers. The time taken to pass the message on will be between 1 and 10 minutes (inclusive), and the number of contacts will range between 0 and one less than the number of stockbrokers. The number of stockbrokers will range from 1 to 100. The input is terminated by a set of stockbrokers containing 0 (zero) people. 


Output 
For each set of data, your program must output a single line containing the person who results in the fastest message transmission, and how long before the last person will receive any given message after you give it to this person, measured in integer minutes. 
It is possible that your program will receive a network of connections that excludes some persons, i.e. some people may be unreachable. If your program detects such a broken network, simply output the message "disjoint". Note that the time taken to pass the message from person A to person B is not necessarily the same as the time taken to pass it from B to A, if such transmission is possible at all. 
Sample Input 
32 2 4 3 52 1 2 3 62 1 2 2 253 4 4 2 8 5 31 5 84 1 6 4 10 2 7 5 202 2 5 1 50 


Sample Output 
3 23 10 


Source 
Southern African 2001 

—————————————————————————————————————————————————— 
Floyd-Warshall算法是解决任意两点间的最短路径的一种算法,可以正确处理有向图(Directed Graph)或负数的代价(negtive cost)的最短路径问题。Floyd-Warshall算法的时间复杂度为<math>O(N^3)</math>。 
 Floyd-Warshall算法的描述如下: 
 
for k  1 to n do
  for i  1 to n do  
    for j  1 to n do
      if (<math>D_{i,k} + D_{k,j} < D_{i,j}</math>) then      
          <math>D_{i,j}</math>  <math>D_{i,k} + D_{k,j}</math>; 
 
其中<math>D_{i,j}</math>表示由点<math>i</math>到点<math>j</math>的代价(cost),当<math>D_{i,j}</math>为 ∞ 表示两点之间没有任何连接(Disconnected)。 

Floyd算法也可以说是动态规划。 


Source
Problem Id:1125  User Id:lnmm 

Memory:84K  Time:0MS 
Language:C++  Result:Accepted 

 1#include"stdio.h" 
 2int a[101][101]; 
 3int i,j,k=0
 4int min; 
 5int max[101]; 
 6int T; 
 7int n,m,temp,to; 
 8int flag; 
 9void main() 
10
11while(scanf("%d",&n)&&n!=0)    //读入一个set的人数 
12
13       for(i=1;i<=n;i++
14     for(j=1;j<=n;j++
15        a[i][j]=32767
16  for(i=1;i<=n;i++
17   a[i][i]=0;              //初识化该set的矩阵 
18  for(i=1;i<=n;i++)           //读入一个set的数据 
19  
20   scanf("%d",&m); 
21   for(j=1;j<=m;j++
22   
23    scanf("%d %d",&to,&temp); 
24    a[i][to]=temp; 
25   }
 
26  }
 
27  for(k=1;k<=n;k++)             //弗洛伊德算法 
28   for(i=1;i<=n;i++
29    for(j=1;j<=n;j++
30    
31     if(a[i][k]!=32767 && a[k][j]!=32767 && a[i][j]>a[i][k]+a[k][j]) 
32      a[i][j]=a[i][k]+a[k][j]; 
33    }
 
34
35         
36  flag=0
37  for(i=1;i<=n;i++)                        //求出从i人开始,谣言传递需要的时间 
38  {   max[i]=0
39   for(j=1;j<=n;j++
40   
41    if(max[i]<a[i][j])max[i]=a[i][j]; 
42   }
 
43       
44  }
 
45   
46  min=32767;                                //计算最小谣言时间
47  for(i=1;i<=n;i++
48   if(min>max[i]) 
49   {min=max[i]; 
50   k=i; 
51   }
 
52  if(min==32767)printf("disjoint.\n");            
53  else printf("%d %d\n",k,min); 
54
55   
56}
 
57     
58}


 

posted on 2007-09-14 02:00 流牛ζ木马 阅读(1802) 评论(2)  编辑 收藏 引用

评论

# re: ACM PKU 1125 Stockbroker Grapevine 图论 Floyd算法 2009-05-10 13:12 朱一帆

我说楼主啊,你能不能不要那么自大啊,你的程序的结果是WA啊!!!  回复  更多评论   

# re: ACM PKU 1125 Stockbroker Grapevine 图论 Floyd算法 2009-05-14 00:09 zx

果然是WA,楼主,要改改啦!  回复  更多评论   


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2007年9月>
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456

导航

统计

公告

MY Email/MSN :mars1021@163.com QQ : 27402040 流牛ζ木马

常用链接

留言簿(6)

随笔档案

相册

搜索

最新随笔

最新评论

阅读排行榜

评论排行榜