• (中等) 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
  • 相关阅读:
    【leetcode】二叉搜索树中的众数
    【leetcode】和为零的N个唯一整数
    【leetcode】统计位数为偶数的数字
    【leetcode】寻找数组的中心索引
    【leetcode】找出给定方程的正整数解
    【leetcode】寻找比目标字母大的最小字母
    【leetcode】找出井字棋的获胜者
    输出0~N之间的素数
    判断正整数是否是素数(数据结构课后练习题2.13a)
    052-85
  • 原文地址:https://www.cnblogs.com/whywhy/p/4338547.html
Copyright © 2020-2023  润新知