Cow Contest POJ - 3660 Floyd算法,关系链图
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头奶牛,依次标号为1~n,每一头奶牛都有一个能力值(不重复)。现在两两之间不重复的进行m场battle,自然,能力值高的奶牛将战胜能力值低的奶牛。给出m场battle的最终胜负结果(a,b)(a为胜者),问:根据已知结
果,有多少头奶牛在整体中的排名能被确定。 思路:首先得明确一个点,。最短路一般是求最短的路径或时间或其他,而这个题却是用最短路的思想来判断两头奶牛之间的关联,首先一个数组ma用于存放念头奶牛之间是否有关联,因为battle有胜负之分,所以关联是单向的。在Floyd算法中只需要判断两点之间是否有单项的关联,最后判断名次时,要明白一头奶牛在整体中的排名能被确定 <=> 它能被x头不同的奶牛直接或间接打败,同时也能直接或间接打败y头奶牛,也就是它与其他奶牛有双向的关联。 代码:
1 #include <cstdio> 2 #include <fstream> 3 #include <algorithm> 4 #include <cmath> 5 #include <deque> 6 #include <vector> 7 #include <queue> 8 #include <string> 9 #include <cstring> 10 #include <map> 11 #include <stack> 12 #include <set> 13 #include <sstream> 14 #include <iostream> 15 #define mod 998244353 16 #define eps 1e-6 17 #define ll long long 18 #define INF 0x3f3f3f3f 19 using namespace std; 20 21 int main() 22 { 23 //n头奶牛,m场对决 24 int n,m; 25 //ma用于存放两头奶牛之间是否有关系。 26 int ma[110][110]; 27 scanf("%d %d",&n,&m); 28 //初始化为0表示都没有关系 29 memset(ma,0,sizeof(ma)); 30 int a,b; 31 for(int i=1;i<=m;i++) 32 { 33 scanf("%d %d",&a,&b); 34 //为1表示a和b之间有关系,而且是a到b单向的关系 35 ma[a][b]=1; 36 } 37 //Floyd算法核心 38 for(int k=1;k<=n;k++) 39 { 40 for(int i=1;i<=n;i++) 41 { 42 for(int j=1;j<=n;j++) 43 { 44 //如果i到k有关系,并且k带j有关系,则i可以与j有关系 45 if(ma[i][k]&&ma[k][j]) 46 { 47 ma[i][j]=1; 48 } 49 } 50 } 51 } 52 //判断排位名次 53 int ans=0; 54 for(int i=1;i<=n;i++) 55 { 56 int num=1; 57 for(int j=1;j<=n;j++) 58 { 59 //如果i能到j,或j能到i,则两者之间有关系 60 if(ma[i][j]||ma[j][i]) 61 { 62 num++; 63 } 64 } 65 //如果i与其他奶牛都有关系,则可以判断i的名次 66 if(num==n) 67 { 68 ans++; 69 } 70 } 71 printf("%d\n",ans); 72 }
转载于:https://www.cnblogs.com/mzchuan/p/11490020.html
Cow Contest POJ - 3660 Floyd算法,关系链图相关推荐
- H - Cow Contest POJ - 3660(Floyd 传递闭包)
H - Cow Contest POJ - 3660 题意: 有 n 头牛比赛,边 1 -> 2 代表 1 能赢 2 ,给你 m 条边,问能确定出多少头牛的名次? 思路: 如果 1->2 ...
- Cow Contest POJ - 3660 And Longest Paths UVA - 10000(弗洛伊德的应用)
Problem Description N ( 1 ≤ N ≤ 100 ) N (1 ≤ N ≤ 100) N(1≤N≤100) cows, conveniently numbered 1.. N 1 ...
- Cow Contest POJ - 3660(floyed求传递闭包)
N (1 ≤ N ≤ 100) cows, conveniently numbered 1-N, are participating in a programming contest. As we a ...
- Cow Contest POJ - 3660
N (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, are participating in a programming contest. As we ...
- POJ3615(Cow Hurdles)图论-Floyd算法JAVA高速IO外挂!
POJ3615(Cow Hurdles) 图论-Floyd算法JAVA高速IO外挂! package classical_algorithm.graph.FloydWarshall; //http:/ ...
- POJ 3660 Cow Contest(传递闭包floyed算法)
Description N (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, are participating in a programming con ...
- poj 3660(Floyd求传递闭包)
Cow Contest Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9317 Accepted: 5249 Descr ...
- 弗洛伊德(Floyd)算法求解图的最短路径
弗洛伊德(Froyd)算法用于求解所有顶点到所有顶点的的最短路径.时间复杂度为O(n^3). 正如我们所知道的,Floyd算法用于求最短路径.Floyd算法可以说是Warshall算法的扩展,三个fo ...
- Silver Cow Party POJ - 3268(正反建图+dijkstra)
One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1-N is going to attend the big cow ...
最新文章
- 如何利用百度API地图进行定位,非gps 定位
- android之启动桌面activity
- shell实例第9讲:判断用户输入的是否为IP地址
- Python 简单网页爬虫学习
- gradle 修改java代码_自定义一个gradle插件动态修改jar包Class文件
- 微信小程序开发【前端+后端(java)】
- tenda无线网卡Linux驱动,Ubuntu 10.04上腾达W541U V2.0 无线网卡驱动的使用
- win10怎么设置开机启动项目_开机启动项怎么设置呢?
- 安装软件出现提示:无法将数值写入键
- 计网-网络号、子网号、主机号以及子网网络地址,子网广播地址的算法
- Doxygen内部结构
- oracle 自动异地备份,实现Oracle异地数据自动备份方案 | 学步园
- 醋泡三宝可以吃出长寿
- 嵌入式 linux yum命令详解
- 课余或者业余学习python,可以嘛?
- 三级等保 服务器开启日志审计功能
- 一次'诡异'的执行SQL报错ORA-03113的问题处理
- 计算机木马的作用,详细介绍计算机木马下篇-1
- 变异系数(测算数据离散程度相对指标)
- 图像旋转中点的公式推导
热门文章
- 数字孪生园区创新实验室落地解决方案
- 虚幻4引擎相比Unity3D强在什么地方?为什么吃鸡游戏都用该款引擎?
- java抽象类的属性_java抽象类详解
- 重启rabbitmq服务
- 正交表的生成工具 allParis 的使用以及遇到的异常 Can‘t open 0204.txt; at release\allpairs.pl line 368.
- Bootstrap 列表组
- 初学者JAVA99乘法表
- 进入Docker容器中修改mysql密码
- html5中audio兼容性,HTML5 Audio API与兼容性
- CUDA程序性能优化 并行归约