• Cow Contest POJ 3660 (Floyed ) (最短路专题)


    题目描述

    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 ≤ A ≤ N; 1 ≤ B ≤ NA ≠ B), 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.

    Input

    * Line 1: Two space-separated integers: N and M
    * Lines 2..M+1: Each line contains two space-separated integers that describe the competitors and results (the first integer, A, is the winner) of a single round of competition: A and B

    Output

    * Line 1: A single integer representing the number of cows whose ranks can be determined

    题目大意:n头牛有 n 个互不相等的等级,给你 n 头牛之间他们等级高低的关系,问你可以确定多少头牛的等级。

    解题思路:一头牛可以确定等级当且仅当这一头牛与其他 n - 1 头牛的关系都确定,我们可以用一幅图来描述这些牛之间的关系,即如果 a 比 b 等级高,即 mp[ a ] [ b ] = 1,反之则为 mp [ a ][ b ] = -1。再通过Floyed 进行有条件的松弛,来得到每头牛之间可以得知的所有关系,最后遍历每一头牛与其他牛之间的关系,如果都存在关系就可以确定这头牛的等级。

    注意建图时要添加双向边。

    代码:

    #include <iostream>
    #include <cstdio>
    #include <fstream>
    #include <algorithm>
    #include <cmath>
    #include <deque>
    #include <vector>
    #include <queue>
    #include <string>
    #include <cstring>
    #include <map>
    #include <stack>
    #include <set>
    #define ll long long
    #define ull unsigned long long 
    #define MOD 998244353 
    #define INF 0x3f3f3f3f
    #define mem(a,x) memset(a,x,sizeof(a))  
    #define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    using namespace std;
    
    int mp[105][105];
    int n,m;
    void Floyed()
    {
        for(int k=1;k<=n;k++){
            for(int i=1;i<=n;i++){
                for(int j=1;j<=n;j++){
                         if(mp[i][k]==1&&mp[j][k]==-1){
                            mp[i][j]=1;
                            mp[j][i]=-1;
                         }else if(mp[j][k]==1&&mp[i][k]==-1){
                            mp[i][j]=-1;
                            mp[j][i]=1;
                         }
            
                }
            }
        }
    }
    int main()
    {
        cin>>n>>m;
        mem(mp,INF);
        for(int i=1;i<=n;i++)mp[i][i]=0;
        for(int i=1;i<=m;i++){
            int a,b;
            scanf("%d %d",&a,&b);
            mp[a][b]=1;
            mp[b][a]=-1;
        }
        Floyed();
       /*/ for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                cout<<mp[i][j]<<" ";
            }
            cout<<endl;
        } /*/
        int sum=0;
        for(int i=1;i<=n;i++){
            int k=1;
            for(int j=1;j<=n;j++){
               if(mp[i][j]==INF){
                  k=0;
                  break;
               }
            }
            if(k){
                sum++;
            }
        }
        cout<<sum;
        return 0;
    }

     

    越自律,越自由
  • 相关阅读:
    屏蔽/捕获并输出错误
    物理机转Hyper-V虚拟机
    Windows Server 2012无法安装 .NET3.5-安装角色或功能失败,找不到源文件
    IDRAC 固件升级操:
    网卡启动安装dell服务器OS
    服务器指定网卡进行备份数据避免影响业务口
    【转载】用户通过WEB方式更改AD域帐户密码
    Windows运维之Windows8.1-KB2999226-x64安装提示 此更新不适用你的计算机
    Exchange 退信550 5.1.11 RESOLVER.ADR.ExRecipNotFound
    优酷kux视频转MP4
  • 原文地址:https://www.cnblogs.com/ha-chuochuo/p/13435560.html
Copyright © 2020-2023  润新知