博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[kuangbin带你飞]专题四 最短路练习 H - Cow Contest (floyed传递背包)
阅读量:4557 次
发布时间:2019-06-08

本文共 2499 字,大约阅读时间需要 8 分钟。

H - Cow Contest

题目链接:

题目:

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 ≤ AN; 1 ≤ BN; AB), 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 54 34 23 21 22 5
Sample Output
2 题意:给出n,m,n是n头牛,m是m对关系,a,b,在前的即为a打败了b,一开始没看懂让我们求啥,然后百度发现用到了传递闭包,有向图的传递背包算法 其中包括了floyed传递背包算法,就是1跟2有关系,2跟3有关系,1也就跟3有关系,算这些牛中能确立多少等级关系,一旦一头牛和其他牛都存在胜负关系, 那么就确立一个等级关系,最后for循环遍历一下就可算出几个等级关系
//// Created by hanyu on 2019/7/20.//#include 
#include
#include
#include
using namespace std;const int maxn=105;#define MAX 0x3f3f3f3fint road[maxn][maxn];int main(){ int n,m; scanf("%d%d",&n,&m); int front,to; memset(road,MAX,sizeof(road)); for(int i=1;i<=n;i++) road[i][i]=0;//自己和自己不成立关系 for(int i=1;i<=m;i++) { scanf("%d%d",&front,&to); road[front][to]=1;//单向图 road[to][front]=-1;//反向不成立 } for(int k=1;k<=n;k++) { for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { if(road[i][k]==road[k][j]&&road[i][k]!=MAX) { road[i][j]=road[i][k]; } } } }//Floyd 传递闭包算法 int cnt,sum=0; for(int i=1;i<=n;i++) { cnt=0; for(int j=1;j<=n;j++) { if(i!=j&&road[i][j]!=MAX) { cnt++; }//遍历每头牛和其他牛的关系 } if(cnt==n-1)//如果关系达到n-1,即和自身之外的其他牛都确定了关系,则可确定等级 sum++; } printf("%d\n",sum); return 0;}

 

 

转载于:https://www.cnblogs.com/Vampire6/p/11219248.html

你可能感兴趣的文章
HttpClient连接超时及读取超时
查看>>
SQL优化方法
查看>>
SEO必须掌握的高级搜索指令
查看>>
生产者消费者模型
查看>>
ORACLE 字符串超长问题解决方案
查看>>
使用ZooKeeper协调多台Web Server的定时任务处理(方案1)
查看>>
20171116 每周例行报告
查看>>
[C#] SHA1校验函数用法
查看>>
linux 下 VMware 提示Unable to change virtual machine power state:
查看>>
洛谷P1585 魔法阵
查看>>
线程 题待做
查看>>
PL/SQL可以连oracle,但是jdbc连不上 【转】
查看>>
使用 highlight.js 在网页中高亮显示java 代码 【原】
查看>>
[转]高颜值、好用、易扩展的微信小程序 UI 库,Powered by 有赞
查看>>
[转]SQL Server如何启用xp_cmdshell组件
查看>>
[转]微擎应用笔记3--manifest.xml文件使用说明
查看>>
Codeforces 1000C Covered Points Count 【前缀和优化】
查看>>
python高效读取文件、文件改写
查看>>
gulp
查看>>
pgsql查询优化之模糊查询
查看>>