Cow Contest(最短路floyed传递闭包)
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
- 题目意思:评定牛的等级,有n头牛,进行了m场比赛,位置在前面的是胜利的牛的编号,如果一头牛和剩下的牛都能确定等级关系,说明可以去确定该牛的等级
,求出可以确定等级的牛的个数- 解题思路:这是一个利用最短路floyed算法的一个传递闭包问题,如果一个点和其余各点都确定关系了,那么这个点的等级就可以确定了。
- #include<stdio.h>
- #include<string.h>
- #include<algorithm>
- using namespace std;
- int n,m;
- int map[200][200];
- int floyd()
- {
- int i,j,k;
- for(k=1; k<=n; k++)
- for(i=1; i<=n; i++)
- for(j=1; j<=n; j++)
- {
- if(map[i][k]&&map[k][j])
- {
- map[i][j]=1;///如果任意两个点能够通过第三个点发生关系,那么说明这两个点也是有关系的
- }
- }
- }
- int main()
- {
- int a,b,i,j,count,sum;
- while(scanf("%d%d",&n,&m)!=EOF)
- {
- memset(map,0,sizeof(map));
- for(i=0; i<m; i++)
- {
- scanf("%d%d",&a,&b);
- map[a][b]=1;///可以确定关系的利用邻接矩阵记录为1
- }
- floyd();
- count=0;
- for(i=1; i<=n; i++)
- {
- sum=0;
- for(j=1; j<=n; j++)
- {
- if(map[i][j]||map[j][i])
- {
- sum++;
- }
- }
- if(sum==n-1)///如果一个点和其余各点的关系确定,那么这个点就可以确定等级
- {
- count++;
- }
- }
- printf("%d\n",count);
- }
- return 0;
- }
Cow Contest(最短路floyed传递闭包)的更多相关文章
- 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求传递闭包(可达矩阵))
Cow Contest Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16341 Accepted: 9146 Desc ...
- Bzoj 1612: [Usaco2008 Jan]Cow Contest奶牛的比赛 传递闭包,bitset
1612: [Usaco2008 Jan]Cow Contest奶牛的比赛 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 891 Solved: 590 ...
- POJ 3660 Cow Contest ( 最短路松弛思想应用 && Floyd求传递闭包 )
题意 : 给出 N 头奶牛在比赛的结果,问你最多的能根据给出结果确定其名次的奶牛头数.结果给出的形式为 A B 代表在比赛当中 A 战胜了 B 分析 : 对于一头奶牛来说,如果我们能确定其他 N - ...
- (poj 3660) Cow Contest (floyd算法+传递闭包)
题目链接:http://poj.org/problem?id=3660 Description N ( ≤ N ≤ ) cows, conveniently numbered ..N, are par ...
- POJ-3660 Cow Contest( 最短路 )
题目链接:http://poj.org/problem?id=3660 Description N (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, ar ...
- POJ 3660 cow contest (Folyed 求传递闭包)
N (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, are participating in a programming contest. As we ...
- Median Weight Bead(最短路—floyed传递闭包)
Description There are N beads which of the same shape and size, but with different weights. N is an ...
- Cow Contest POJ - 3660 floyd传递闭包
#include<iostream> #include<cstring> using namespace std; ,INF=0x3f3f3f3f; int f[N][N]; ...
随机推荐
- Flask第三方组件之flask_session
flask默认提供了session, 但是存在以下问题: ① session数据存在客户端, 不安全 ② 大小有限制 ③ 增加了客户端的压力 所以才产生了很多第三方的session机制, 我使用的是f ...
- hadoop学习笔记——zookeeper平台搭建
zookeeper是一个自动管理分布式集群的一个工具,以实现集群的高可用. 比如集群中的一个机器挂掉了,没有zookeeper的话就得考虑挂一个机器对剩下集群工作的影响,而有了zookeeper,它就 ...
- Python学习手册之内部方法、操作符重载和对象生命周期
在上一篇文章中,我们介绍了 Python 的类和继承,现在我们介绍 Python 的内部方法.操作符重载和对象生命周期. 查看上一篇文章请点击:https://www.cnblogs.com/dust ...
- 主存和cache的地址映射
cache是一种高速缓冲寄存器,是为解决CPU和主存之间速度不匹配而采用的一项重要技术. 主存与cache的地址映射方式有全相联方式.直接方式和组相联方式三种. 直接映射(directmapping) ...
- 浅谈ConcurrentHashMap实现原理
我们都知道HashTable是线程安全的类,因为使用了Synchronized来锁整张Hash表来实现线程安全,让线程独占: ConcurrentHashMap的锁分离技术就是用多个锁来控制对Hash ...
- Hotspot优化指南(上)
一次偶然,博主在浏览docs.oracle.com/javase的时候发现了<Hotspot虚拟机垃圾收集调优指南>这篇文档.内心百感交集,之前在看完了周志明的<深入理解Java虚拟 ...
- Java 高级应用编程 第二章 集合
一.Java 中的集合类 1.集合概述 Java中集合类是用来存放对象的 集合相当于一个容器,里面包容着一组对象 —— 容器类 其中的每个对象作为集合的一个元素出现 Java API提供的集合类位于j ...
- 20145226夏艺华 《Java程序设计》第1周学习总结
http://www.cnblogs.com/bestixyh/p/5779286.html 去年暑假写的,确实比较丑陋,保留下来也是为了激励自己作出更多改变.寒假写的每一篇博客都尽最大努力养成了良好 ...
- 实现动态的XML文件读写操作(依然带干货)
前言 最近由于项目需求,需要读写操作XML文件,并且存储的XML文件格式会随着导入的数据不同而随时改变(当然导入的数据还是有一定约束的),这样我们要预先定义好XML文件的格式就不太现实了,如何实现不管 ...
- stm32中如何进行printf重定向用于串口调试输出
1 在main中包含stdio.h 文件 2 Target选项框里选Use MicroLib 选项 3 在main中添加UART1_Configuration()初始化的代码 Uart1初始化,voi ...