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个 然后每次减掉上一批的人数 麻烦的很 复杂度上天了.... 正难则反 ...
随机推荐
- linux下golang的配置
linux下golang的配置 之前开发golang一直在windows下,今天在linux下试了一下 ,遇到一些梗,比如go 找不到 sync包.花了一小时全部解决,把过程记录一下. 安装 go 我 ...
- spring heibernate 调用存储过程
一:参考网址 http://sunbin123.iteye.com/blog/1007556 二:示例 @Autowired @Qualifier("jdbcTemplate") ...
- DTCMS展示一级栏目并展示各自栏目下的二级栏目
c#代码中 <!--C#代码--> <%csharp%> string parent_id=DTRequest.GetQueryString("parent_id&q ...
- *HTML5 新元素
HTML5 新元素 自1999年以后HTML 4.01 已经改变了很多,今天,在HTML 4.01中的几个已经被废弃,这些元素在HTML5中已经被删除或重新定义. 为了更好地处理今天的互联网应用,HT ...
- 解决Ajax跨域问题:Origin xx is not allowed by Access-Control-Allow-Origin.
一:使用jsonp格式, 如jquery中ajax请求参数 dataType:'JSONP'. <html> <head> <title>title</t ...
- SharedPreference 的存取
1.通过名称来获取指定的SharedPreferences,下面这句代码表示获取名字问hello的SharedPreferences,数据保存在 data/data/package名/shared_p ...
- 完美解决IE6中fixed抖动问题的方法
我们可以通过position:fixed来实现元素的固定效果,如网页中底部的"回到顶部菜单",底部的toolbar,对联广告等等,可惜fixed属性在IE6及以下是不支持的.通常的 ...
- 部门招聘开发人员(python相关)
岗位职责: 1.参与需求分析,产品设计,功能开发: 2.负责系统平台的日常维护: 3.与团队技术交流,共同进步 任职要求: 1.精通Python:对Python有兴趣. 2.熟悉MVC架构,精通Dja ...
- pyunit实现数据测试框架
PyUnit提供的动态方法,只编写一个测试类来完成对整个软件模块的测试,这样对象的初始化工作可以在setUp()方法中完成,而资源的释放则可以在tearDown()方法中完成. 使用PyUnit可以像 ...
- WPF中的文字修饰
我们知道,文字的修饰包括:空心字.立体字.划线字.阴影字.加粗.倾斜等.这里只说划线字的修饰方式,按划线的位置,我们可将之分为:上划线.中划线.基线与下划线.如图: 从上至下,分别为上划线(Overl ...