XMU 1050 Diffuse Secret 【最短路】

探讨了在一个社交网络中,每个人将自己所知的秘密每天与其朋友分享的情况下,所有人得知所有秘密所需的时间。通过Floyd算法求解最短路径问题,以确定秘密传播的最长时间。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1050: Diffuse Secret

Time Limit: 500 MS  Memory Limit: 64 MB
Submit: 10  Solved: 8
[Submit][Status][Web Board]

Description

  A secret is good in one, better in two, ill in three and worse in four. 
  ----Hi, I know a secret, come on I will tell you. But you must promise that you won’t tell it to other guys. 
  ----OK, of course I won't.
 
  Do you think they will keep the secret? No no, just like most people, they share their secrets with their own friends. If everybody knows a secret, it will never be secret any more. 
  For some reasons that know to all, TheBeet keep the salaries of employees as secret. But the employees don’t. At the beginning, everyone only knows how much he/she get. Everyday they share every secret they know with their friends (They don’t share the secret that they just heard today). For Example, A and B are good friends, B and C are good friends, A knows the secret a and secret d. B knows the secret b and secret c. C knows the secret e. After they share their secrets, A will know the secret abcd, B will know the secret abcde, C will know the secret bce. Today TheBeet increases every employee’s salary and he wonders when will be known to all employees.

Input

  The input file contains several test cases. 
  Each test case begins with two integers N, M 0 < N <= 100, 0 <= M <= (N * (N – 1)), N is the number of employee and M indicates there are M pairs of friend. 
  The following M lines contain 2 integers each line describing the pair of friends. 
N = 0 and M = 0 indicates the end of input and should not be processed.

Output

  For each test case, for each test case, output “Case #:” on the first line, '#' is the number of the test case. Then output a single integer as after such days, these secrets will never be secret. If there is one secret that some one will never know, you just output “Secret.” (Without quotes)

Sample Input

5 5
1 2
2 3
3 4
4 5
5 1
3 1
1 2
0 0

Sample Output

Case 1:
2
Case 2:
Secret.

HINT

 

Source

[ Submit][ Status][ Web Board]

 

 

题目链接:

  http://acm.xmu.edu.cn/JudgeOnline/problem.php?id=1050

题目大意:

  每个人都有个秘密,他们每一天都会把自己的所有知道的秘密与他朋友分享

  当天得到的秘密不会分享。问所有人都知道所有秘密时是第几天。

题目思路:

  【最短路】

  考虑第i个人的秘密最迟到j,则秘密从i到j的传播为从i到j的最短路。

  那么所有人的秘密都到达最远的人的时间就为答案,即求所有人到所有人的最短路里的最大值。

  用floyd即可。

 

 1 /****************************************************
 2     
 3     Author : Coolxxx
 4     Copyright 2017 by Coolxxx. All rights reserved.
 5     BLOG : http://blog.youkuaiyun.com/u010568270
 6     
 7 ****************************************************/
 8 #include<bits/stdc++.h>
 9 #pragma comment(linker,"/STACK:1024000000,1024000000")
10 #define abs(a) ((a)>0?(a):(-(a)))
11 #define lowbit(a) (a&(-a))
12 #define sqr(a) ((a)*(a))
13 #define mem(a,b) memset(a,b,sizeof(a))
14 const double EPS=1e-8;
15 const int J=10000;
16 const int MOD=100000007;
17 const int MAX=0x7f7f7f7f;
18 const double PI=3.14159265358979323;
19 const int N=104;
20 using namespace std;
21 typedef long long LL;
22 double anss;
23 LL aans;
24 int cas,cass;
25 int n,m,lll,ans;
26 int e[N][N],f[N][N];
27 void floyd()
28 {
29     int i,j,k;
30     for(k=1;k<=n;k++)
31     {
32         for(i=1;i<=n;i++)
33         {
34             if(i==k)continue;
35             for(j=1;j<=n;j++)
36             {
37                 if(i==j || k==j)continue;
38                 f[i][j]=min(f[i][j],f[i][k]+f[k][j]);
39             }
40         }
41     }
42 }
43 int main()
44 {
45     #ifndef ONLINE_JUDGE
46     freopen("1.txt","r",stdin);
47 //    freopen("2.txt","w",stdout);
48     #endif
49     int i,j,k,l;
50     int x,y,z;
51 //    for(scanf("%d",&cass);cass;cass--)
52 //    for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
53 //    while(~scanf("%s",s))
54     while(~scanf("%d",&n))
55     {
56         scanf("%d",&m);
57         if(!n && !m)break;
58         ans=0;mem(f,0x01);
59         printf("Case %d:\n",++cass);
60         for(i=1;i<=m;i++)
61         {
62             scanf("%d%d",&x,&y);
63             f[x][y]=f[y][x]=e[x][y]=e[y][x]=1;
64         }
65         floyd();
66         for(i=1;i<=n;i++)
67         {
68             for(j=1;j<=n;j++)
69             {
70                 if(i==j)continue;
71                 ans=max(ans,f[i][j]);
72             }
73         }
74         if(ans==0x01010101)puts("Secret.");
75         else printf("%d\n",ans);
76     }
77     return 0;
78 }
79 /*
80 //
81 
82 //
83 */
View Code

 

转载于:https://www.cnblogs.com/Coolxxx/p/6691642.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值