(中等) POJ 3660 Cow Contest,Floyd。

  Description

  N (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, are participating in a programming contest. As we all know, some cows code better than others. Each cow has a certain constant skill rating that is unique among the competitors.

  The contest is conducted in several head-to-head rounds, each between two cows. If cow A has a greater skill level than cow B (1 ≤ AN; 1 ≤ BN; AB), then cow A will always beat cow B.

  Farmer John is trying to rank the cows by skill level. Given a list the results of M (1 ≤ M ≤ 4,500) two-cow rounds, determine the number of cows whose ranks can be precisely determined from the results. It is guaranteed that the results of the rounds will not be contradictory.

  题目就是求有几头牛的排名确定了,用Floyd算法,如果a胜了b,就建边a->b,然后Floyd,然后枚举每一个点,对于这个点,如果所有其余的点与他的位置都确定了的话(也就是两点之间的最短路不为INF),那么这个点就是位置确定的点。

代码如下:

#include<iostream>
#include<cstring>
#include<cstdio>

using namespace std;

const int INF=10e8;

int N,M;
int map1[110][110];

int main()
{
    int a,b;
    int ans;
    bool ok;

    while(cin>>N>>M)
    {
        for(int i=1;i<=N;++i)
            for(int j=1;j<=N;++j)
                map1[i][j]= i==j ? 0 : INF;

        for(int i=1;i<=M;++i)
        {
            cin>>a>>b;
            map1[a][b]=1;
        }

        for(int k=1;k<=N;++k)
            for(int i=1;i<=N;++i)
                for(int j=1;j<=N;++j)
                    map1[i][j]=min(map1[i][j],map1[i][k]+map1[k][j]);

        ans=N;

        for(int i=1;i<=N;++i)
        {
            ok=1;

            for(int j=1;j<=N;++j)
                if(map1[i][j]==INF && map1[j][i]==INF)
                {
                    ok=0;
                    break;
                }

            if(!ok)
                --ans;
        }

        cout<<ans<<endl;
    }

    return 0;
}
View Code
原文地址:https://www.cnblogs.com/whywhy/p/4338547.html