POJ 3660 Cow Contest (Floyd)

题目链接:http://poj.org/problem?id=3660

题意是给你n头牛,给你m条关系,每条关系是a牛比b牛厉害,问可以确定多少头牛的排名。

要是a比b厉害,a到b上就建一条有向边......这样建好之后,如果比a牛厉害的牛都能达到a牛,而a牛能到达比a牛差的牛的话,牛的头数又恰好是n-1头,那么a牛的排名则是确定的。

所以用Flody比较方便,要是i到k能到达,k到j能到达,那么i到j就能到达,i就比j厉害。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 const int MAXN = 105;
 6 bool cost[MAXN][MAXN];
 7 
 8 void init() {
 9     memset(cost , false , sizeof(cost));
10 }
11 
12 int main()
13 {
14     int n , m , u , v;
15     while(cin >> n >> m) {
16         init();
17         while(m--) {
18             cin >> u >> v;
19             cost[u][v] = true;
20         }
21         for(int k = 1 ; k <= n ; k++) {
22             for(int j = 1 ; j <= n ; j++) {
23                 for(int i = 1 ; i <= n ; i++) {
24                     if(cost[i][k] && cost[k][j]) 
25                         cost[i][j] = true;
26                 }
27             }
28         }
29         int cont , res = 0;
30         for(int i = 1 ; i <= n ; i++) {
31             cont = 0;
32             for(int j = 1 ; j <= n ; j++) {
33                 if(cost[i][j] != cost[j][i])
34                     cont += 1;
35             }
36             if(cont == n - 1)
37                 res++;
38         }
39         cout << res << endl;
40     }
41 }
原文地址:https://www.cnblogs.com/Recoder/p/5311452.html