POJ3660——Cow Contest(Floyd+传递闭包)
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+传递闭包)的更多相关文章
- POJ3660 Cow Contest —— Floyd 传递闭包
题目链接:http://poj.org/problem?id=3660 Cow Contest Time Limit: 1000MS Memory Limit: 65536K Total Subm ...
- POJ-3660 Cow Contest Floyd传递闭包的应用
题目链接:https://cn.vjudge.net/problem/POJ-3660 题意 有n头牛,每头牛都有一定的能力值,能力值高的牛一定可以打败能力值低的牛 现给出几头牛的能力值相对高低 问在 ...
- POJ3660 Cow Contest floyd传递闭包
Description N (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, are participating in a programming con ...
- POJ3660:Cow Contest(Floyd传递闭包)
Cow Contest Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16941 Accepted: 9447 题目链接 ...
- POJ-3660.Cow Contest(有向图的传递闭包)
Cow Contest Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 17797 Accepted: 9893 De ...
- ACM: POJ 3660 Cow Contest - Floyd算法
链接 Cow Contest Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Descri ...
- POJ 3660 Cow Contest(传递闭包floyed算法)
Cow Contest Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5989 Accepted: 3234 Descr ...
- POJ 3660 Cow Contest【传递闭包】
解题思路:给出n头牛,和这n头牛之间的m场比赛结果,问最后能知道多少头牛的排名. 首先考虑排名怎么想,如果知道一头牛打败了a头牛,以及b头牛打赢了这头牛,那么当且仅当a+b+1=n时可以知道排名,即为 ...
- poj 3660 Cow Contest (传递闭包)
/* floyd 传递闭包 开始Floyd 之后统计每个点能到的或能到这个点的 也就是他能和几个人确定胜负关系 第一批要有n-1个 然后每次减掉上一批的人数 麻烦的很 复杂度上天了.... 正难则反 ...
随机推荐
- JAVA中的代理技术(静态代理和动态代理)
最近看书,有两个地方提到了动态代理,一是在Head First中的代理模式,二是Spring AOP中的AOP.所以有必要补充一下动态代理的相关知识. Spring采用JDK动态代理和CGLib动态代 ...
- UserControl eventhander 注册问题
1. 如果主页面调用UserControl来画界面 2. UserControl局部变化需要通过事件通知主页面,通过UserControl定义EventHandler,主界面注册UserControl ...
- dapper extensions (predicates)
https://github.com/tmsmith/Dapper-Extensions/wiki/Predicates The predicate system in Dapper Extensio ...
- OSI与TCP/IP模型之比较
摘要:OSI参考模型和Internet模型(或称TCP/IP模型)作为计算网络发展过程影响力大的两大模型,它们共同之处是:都采用了层次结构的概念,从分析两者的异同入手,找出OSI的消亡和Interne ...
- Treimu更新记录1.2.9.0
Treimu是一个WPF音乐播放器个人小项目.程序集文件:http://pan.baidu.com/s/1pJLSHsB项目源代码:http://pan.baidu.com/s/1jGHtjfC 1. ...
- UartDma工作方式
一.初始化 1.初始化串口,时钟 MX_USART1_UART_Init(); 串口时钟初始化为内部时钟 PeriphClkInit.Usart1ClockSelection = RCC_USART1 ...
- C语言数据结构之栈:中缀表达式的计算
*注:本人技术不咋的,就是拿代码出来和大家看看,代码漏洞百出,完全没有优化,主要看气质,是吧 学了数据结构——栈,当然少不了习题.习题中最难的也是最有意思的就是这个中缀表达式的计算了(可以算+-*/和 ...
- 关于CSS的图像放大问题的解决,需要借助jQuery等直接用css3设置
W3C标准中对css3的transition这是样描述的:“css的transition允许css的属性值在一定的时间区间内平滑地过渡.这种效果可以在鼠标单击.获得焦点.被点击或对元素任何改变中触发, ...
- RecursiveDirectoryIterator目录操作类
/** * @author Funsion Wu * @abstract SPL使用案例,全国首发,技术分享,欢迎转帖 */ class Dir extends RecursiveDirectoryI ...
- WCF 动态生成 不用增加引用两种方式
一.fromwork2.0低版本方式 1,打开vs的命令工具 输入:wsdl wcf地址 + /l:cs /out:文件名 上面红色部分替换掉就行,文件名,你想叫什么文件名都行. 2,回车,生成的文件 ...