(poj 3660) Cow Contest (floyd算法+传递闭包)
题目链接:http://poj.org/problem?id=3660
Description N ( ≤ N ≤ ) cows, conveniently numbered ..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 ( ≤ A ≤ N; ≤ 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 ( ≤ M ≤ ,) 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 : Two space-separated integers: N and M
* Lines ..M+: 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 : A single integer representing the number of cows whose ranks can be determined
Sample Input Sample Output Source
USACO January Silver
题目大意:有N头牛,每头牛都有个特技。有M场比赛,比赛形式是A B 意味着牛A一定能赢牛B,问在M场比赛后,有几头牛的名次是确定的(不互相矛盾)?
方法:这是最短路练习,但是以最短路的形式无法求出来,看了一下题解发现是最短路的floyd算法写的,这个算法的时间复杂度为O(n3),自己百度了一下这个算法,再求每个点的出度入度的和,如果等于n(算上自己),就能确定
#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<math.h>
#include<queue>
using namespace std;
#define INF 0x3f3f3f3f
#define ll long long
#define met(a,b) memset(a,b,sizeof(a))
#define N 500
int a[N][N],G[N][N];
int main()
{
int n,m,e,v;
while(scanf("%d %d",&n,&m)!=EOF)
{
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
a[i][j]=i==j?:;
}
for(int i=;i<m;i++)
{
scanf("%d %d",&e,&v);
a[e][v]=;
}
///floyd算法 五行代码 三层for循环 时间复杂度为O(n3)
for(int k=;k<=n;k++)
{
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
if(a[i][j]||(a[i][k] && a[k][j]))
a[i][j]=;
}
}
}
///求每个点的出度入度之和
int ans=,sum=;
for(int i=;i<=n;i++)
{
sum=;
for(int j=;j<=n;j++)
{
if(a[i][j] || a[j][i])
sum++;
}
if(sum==n)
ans++;
}
printf("%d\n",ans);
}
return ;
}
/*
5 5
4 3
4 2
3 2
1 2
2 5
*/
(poj 3660) Cow Contest (floyd算法+传递闭包)的更多相关文章
- ACM: POJ 3660 Cow Contest - Floyd算法
链接 Cow Contest Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Descri ...
- POJ 3660 Cow Contest (Floyd)
题目链接:http://poj.org/problem?id=3660 题意是给你n头牛,给你m条关系,每条关系是a牛比b牛厉害,问可以确定多少头牛的排名. 要是a比b厉害,a到b上就建一条有向边.. ...
- POJ 3660 Cow Contest / HUST 1037 Cow Contest / HRBUST 1018 Cow Contest(图论,传递闭包)
POJ 3660 Cow Contest / HUST 1037 Cow Contest / HRBUST 1018 Cow Contest(图论,传递闭包) Description N (1 ≤ N ...
- POJ 3660 Cow Contest 传递闭包+Floyd
原题链接:http://poj.org/problem?id=3660 Cow Contest Time Limit: 1000MS Memory Limit: 65536K Total Subm ...
- POJ 3660 Cow Contest
题目链接:http://poj.org/problem?id=3660 Cow Contest Time Limit: 1000MS Memory Limit: 65536K Total Subm ...
- POJ 3660—— Cow Contest——————【Floyd传递闭包】
Cow Contest Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit ...
- POJ 3660 Cow Contest(传递闭包floyed算法)
Cow Contest Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5989 Accepted: 3234 Descr ...
- POJ 3660 Cow Contest(Floyd求传递闭包(可达矩阵))
Cow Contest Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16341 Accepted: 9146 Desc ...
- POJ - 3660 Cow Contest 传递闭包floyed算法
Cow Contest POJ - 3660 :http://poj.org/problem?id=3660 参考:https://www.cnblogs.com/kuangbin/p/31408 ...
- poj 3660 Cow Contest(传递闭包 Floyd)
链接:poj 3660 题意:给定n头牛,以及某些牛之间的强弱关系.按强弱排序.求能确定名次的牛的数量 思路:对于某头牛,若比它强和比它弱的牛的数量为 n-1,则他的名次能够确定 #include&l ...
随机推荐
- 如何获取网页验证码图片并保存到本地(Java实现) [问题点数:40分,结帖人lanxuezaipiao]
http://bbs.csdn.net/topics/390426978 public static String readCheckImage(HashMap<String, String&g ...
- JDK1.5与1.6在Override上的区别
@Override是JDK5 就已经有了,但有个小小的Bug,就是不支持对接口的实现,认为这不是Override 而JDK6 修正了这个Bug,无论是对父类的方法覆盖还是对接口的实现都可以加上@Ove ...
- nodejs package.json详细解读
package.json详细内容 它是这样一个json文件(注意:json文件内是不能写注释的,复制下列内容请删除注释): JavaScript { "name": "t ...
- 规划(纪念我在ACM道路上的一年)
现在已经是晚上一点了,我早早的躺在床上,不能入睡,因为睡觉前看了一下我们学校今年区域赛的成绩总结,派出八次队伍,七个铜-- 再加上这两天ACM迎新杯的筹备过程的问题,让我产生了深深的思考-- 去年司老 ...
- Java程序输出打字
代码: 效果:
- VMware设置NAT网络
很多初学者迷茫与如何实现虚拟机VMware与主机互联,这里小编介绍下简单实用的NAT网络. 工具/原料 VMware 方法/步骤 打开VMware,选择 编辑, 虚拟网络编辑器 默认情况下, ...
- js获取当前日期与星期
var currentDate = new Date(); var weekday = ["星期日", "星期一", "星期二", &quo ...
- linux视频学习3(linux安装,shell,tcp/ip协议,网络配置)
linux系统的安装: 1.linux系统的安装方式三种: 1.独立安装linux系统. 2.虚拟机安装linux系统. a.安装虚拟机,基本是一路点下去. b.安装linux. c.linux 安装 ...
- [转]SSL协议详解
背景介绍 最近在看<密码学与网络安全>相关的书籍,这篇文章主要详细介绍一下著名的网络安全协议SSL. 在开始SSl介绍之前,先给大家介绍几个密码学的概念和相关的知识. 1.密 ...
- cc2530 -----SampleApp.c解析
/************************************************************************************************** ...