Cow Contest

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 ≤ A ≤ N; 1 ≤ B ≤ N; A ≠ 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
Sample Input
5 5
4 3
4 2
3 2
1 2
2 5
Sample Output
2

题目大意:

    给一些牛的排名关系,问有多少牛的排名确定。

解题思路:

    使用Floyd算法来判断传递闭包。

    首先通过输入信息建立邻接矩阵,再使用Floyd求出最短路径Edge[i][j]。

    这时,相对于牛i若Edge[i][j]存在,则说明i肯定打不过j。若Edge[j][i]存在则说明i肯定打得过牛j。

    若i肯定能打过的牛和肯定打不过的牛的和等于牛的总和N-1,则牛i的位置确定。

    据说这个东西叫做传递闭包--!

Code:

 #include<stdio.h>
#include<string>
#include<iostream>
#define MAXN 300
using namespace std;
int edge[MAXN+][MAXN+];
int N,M;
void init()
{
for (int i=; i<=N; i++)
for (int j=; j<=N; j++)
edge[i][j]=INT_MAX;
}
void floyd()
{
int m,i,j;
for (m=; m<=N; m++)
for (i=; i<=N; i++)
for (j=; j<=N; j++)
{
if (edge[i][m]!=INT_MAX&&edge[m][j]!=INT_MAX&&edge[i][j]>edge[i][m]+edge[m][j])
edge[i][j]=edge[i][m]+edge[m][j];
}
}
int main()
{
while (cin>>N>>M)
{
init();
for (int i=; i<=M; i++)
{
int x1,x2;
scanf("%d %d",&x1,&x2);
edge[x1][x2]=;
}
floyd();
int sum=;
for (int i=; i<=N; i++)
{
int cnt=;
for (int j=; j<=N; j++)
{
if (i!=j&&edge[i][j]!=INT_MAX)
cnt++;
if (i!=j&&edge[j][i]!=INT_MAX)
cnt++;
}
if (cnt==N-) sum++;
}
printf("%d\n",sum);
}
return ;
}

POJ3660——Cow Contest(Floyd+传递闭包)的更多相关文章

  1. POJ3660 Cow Contest —— Floyd 传递闭包

    题目链接:http://poj.org/problem?id=3660 Cow Contest Time Limit: 1000MS   Memory Limit: 65536K Total Subm ...

  2. POJ-3660 Cow Contest Floyd传递闭包的应用

    题目链接:https://cn.vjudge.net/problem/POJ-3660 题意 有n头牛,每头牛都有一定的能力值,能力值高的牛一定可以打败能力值低的牛 现给出几头牛的能力值相对高低 问在 ...

  3. POJ3660 Cow Contest floyd传递闭包

    Description N (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, are participating in a programming con ...

  4. POJ3660:Cow Contest(Floyd传递闭包)

    Cow Contest Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16941   Accepted: 9447 题目链接 ...

  5. POJ-3660.Cow Contest(有向图的传递闭包)

      Cow Contest Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17797   Accepted: 9893 De ...

  6. ACM: POJ 3660 Cow Contest - Floyd算法

    链接 Cow Contest Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Descri ...

  7. POJ 3660 Cow Contest(传递闭包floyed算法)

    Cow Contest Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5989   Accepted: 3234 Descr ...

  8. POJ 3660 Cow Contest【传递闭包】

    解题思路:给出n头牛,和这n头牛之间的m场比赛结果,问最后能知道多少头牛的排名. 首先考虑排名怎么想,如果知道一头牛打败了a头牛,以及b头牛打赢了这头牛,那么当且仅当a+b+1=n时可以知道排名,即为 ...

  9. poj 3660 Cow Contest (传递闭包)

    /* floyd 传递闭包 开始Floyd 之后统计每个点能到的或能到这个点的 也就是他能和几个人确定胜负关系 第一批要有n-1个 然后每次减掉上一批的人数 麻烦的很 复杂度上天了.... 正难则反 ...

随机推荐

  1. HTTP Error 500.21解决方案

    Windows 7 IIS (HTTP Error 500.21 - Internal Server Error)解决方案   今天在测试网站的时候,在浏览器中输入http://localhost/时 ...

  2. AngularJS(10)-数据验证

    AngularJS 表单和控件可以提供验证功能,并对用户输入的非法数据进行警告.客户端的验证不能确保用户输入数据的安全,所以服务端的数据验证也是必须的. <!DOCTYPE html> & ...

  3. php curl抓取远程页面内容的代码

    使用php curl抓取远程页面内容的例子. 代码如下: <?php /** * php curl抓取远程网页内容 * edit by www.jbxue.com */ $curlPost = ...

  4. IOS系统框架

    IOS系统框架:ios架构号MAC OS的基础框架相似:ios扮演底层硬件和应用程序的中介,底层为所有应用程序提供基础服务,高层则包含一些复杂巧妙的技术和服务,并且创建的应用程序不能直接访问硬件. C ...

  5. java----线程篇

    一个线程是进程内的一个单一的顺序控制流程图,多线程是指一个进程可以同时运行几个任务,每个任务由一个线程来完成.即多个线程可以同时运行,并且在一个进程内执行不同的任务. 1.创建线程两种方法方法一, 继 ...

  6. 内核升级修复nfs

    Not starting NFS kernel daemon: no support in current kernel. sudo gedit /etc/init.d/nfs-kernel-serv ...

  7. MySQL 5.1.63 单机配置多实例(简单配置)

    需求: 在一台服务器上通过源码编译安装一个版本为5.1.63版本MySQL数据库: 方案:将所有配置文件与数据等均存放在/home/zhaoshuangshuang下.在同一个MySQL中运行两个实例 ...

  8. GNU_makefile_template

    #g++ compiler: options # -std=c++0x enables ISO C++ 11 standard # -I.. pulls in the Version_test.h f ...

  9. 【ASP.NET】TreeView控件学习

    相关链接 : http://www.cnblogs.com/yc-755909659/p/3596039.html

  10. Hadoop的安装与配置说明

    安装hadoop是一件非常容易的事情,读者可以在官网上下载到最近的几个hadoop版本.网址为http://apache.etoak.com//hadoop/core/.   目前,hadoop可以运 ...