Fork me on GitHub

HDU ACM 1869 六度分离(Floyd)

六度分离

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2927    Accepted Submission(s): 1127

【题目链接】http://acm.hdu.edu.cn/showproblem.php?pid=1869

【解题思路】Floyd算法,要证明两个素不相识的人通过中间的六个人就能联系起来,这就意味着这两个素不相识的人之间的最短距离不超过7,用Floyd算出每两个人之间的距离,然后在素不相识的情况下判断两个人之间的距离是否小于等于7,否则就不满足“六度分离”理论

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <string>
 4 #include <cstring>
 5 #include <map>
 6 #include <algorithm>
 7 #include <cmath>
 8 
 9 #define NV 101
10 #define NE 202
11 
12 using namespace std;
13 
14 const int INF = 1<<30;
15 const double eps = 1e-8;
16 int ne, nv;
17 int Rate[NV][NV];
18 bool con[NV][NV];
19 map<string, int> simap;
20 
21 bool Floyd()
22 {        
23     for(int k = 0; k < nv; ++k)
24     for(int i = 0; i < nv; ++i)
25         if(Rate[i][k] < INF)
26         for(int j = 0; j < nv; ++j)
27             if(Rate[k][j] < INF && Rate[i][j] > Rate[i][k] + Rate[k][j])
28             Rate[i][j] = Rate[i][k] + Rate[k][j];
29         for(int i = 0; i < nv; ++i)
30         for(int j = i; j < nv; ++j)
31         {
32             if(i != j && !con[i][j] && Rate[i][j] > 7)
33             return false;
34         }
35     return true;
36 }
37 
38 int main()
39 {
40     #ifndef ONLINE_JUDGE
41     freopen("input.txt", "r", stdin);
42     #endif
43     while(cin >> nv >> ne)
44     {
45         for(int i=0; i<nv; ++i)
46         for(int j=i; j<nv; ++j)
47         {
48             Rate[i][j] = Rate[j][i] = INF;
49             con[i][j] = con[j][i] = false;
50         }
51             
52         for(int i=0, u, v; i<ne; ++i)
53         {
54             cin >> u >> v;
55             Rate[u][v] = Rate[v][u] = 1;
56             con[u][v] = con[v][u] = true;
57         }
58         if(Floyd()) printf("Yes\n");
59         else printf("No\n");
60     }    
61     return 0;
62 }

 

posted @ 2013-07-30 13:22  Gifur  阅读(536)  评论(0编辑  收藏  举报
TOP