poj 3660 Cow Contest 夜

http://poj.org/problem?id=3660

此题不是最短路 但可以用到 floyd

A牛可以打败B牛 B牛可以打败C牛 那么A牛一定可以打败C牛

讲这个关系进行传递

每一头牛打败别的牛(i头)和被别的牛打败(j头)的和(i+j)必须是n-1 才能确定其排名

#include<iostream>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdio>

using namespace std;

const int N=105;

bool beat[N][N];
int sum[N];
int main()
{
    int n,m;
    while(cin>>n>>m)
    {
        int i,j;
        memset(beat,false,sizeof(beat));
        while(m--)
        {
            cin>>i>>j;
            beat[i][j]=true;
        }
        for(int l=1;l<=n;++l)
        {
            for(int i=1;i<=n;++i)
            {
                for(int j=1;j<=n;++j)
                {
                    if(beat[i][l]&&beat[l][j])
                    {
                        beat[i][j]=true;
                    }
                }
            }
        }
        memset(sum,0,sizeof(sum));
        for(int i=1;i<=n;++i)
        {
            for(int j=1;j<=n;++j)
            {
                if(beat[i][j])
                {
                    ++sum[i];
                    ++sum[j];
                }
            }
        }
        int ans=0;
        for(int i=1;i<=n;++i)
        {
            if(sum[i]==n-1)
            {
                ++ans;
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

  

原文地址:https://www.cnblogs.com/liulangye/p/2484974.html