图论---POJ 3660 floyd 算法(模板题)
是一道floyd变形的题目。题目让确定有几个人的位置是确定的,如果一个点有x个点能到达此点,从该点出发能到达y个点,若x+y=n-1,则该点的位置是确定的。用floyd算发出每两个点之间的距离,最后统计时,若dis[a][b]之间无路且dis[b][a]之间无路,则该点位置不能确定。最后用点个数减去不能确定点的个数即可。题目:
| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 4813 | Accepted: 2567 |
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
ac代码:
- #include <iostream>
- #include <string.h>
- #include <cstdio>
- using namespace std;
- #define MAX 0x7fffffff
- const int N=110;
- int dis[N][N];
- int n,m;
- void floyd(){
- for(int k=1;k<=n;++k){
- for(int i=1;i<=n;++i){
- for(int j=1;j<=n;++j){
- if(dis[i][k]!=MAX&&dis[k][j]!=MAX&&dis[i][j]>dis[i][k]+dis[k][j]){
- dis[i][j]=dis[i][k]+dis[k][j];
- }
- }
- }
- }
- }
- int main(){
- //freopen("1.txt","r",stdin);
- while(~scanf("%d%d",&n,&m)){
- for(int i=1;i<=n;++i){
- for(int j=1;j<=n;++j)
- dis[i][j]=MAX;
- }
- int x,y;
- while(m--){
- scanf("%d%d",&x,&y);
- dis[x][y]=1;
- }
- floyd();
- int ans=0;
- for(int i=1;i<=n;++i){
- for(int j=1;j<=n;++j){
- if(j==i)continue;
- if(dis[i][j]==MAX&&dis[j][i]==MAX)
- {ans++;break;}
- }
- }
- printf("%d\n",n-ans);
- }
- return 0;
- }
版权声明:本文为博主原创文章,未经博主允许不得转载。
图论---POJ 3660 floyd 算法(模板题)的更多相关文章
- POJ 3041 匈牙利算法模板题
一开始预习是百度的算法 然后学习了一下 然后找到了学长的ppt 又学习了一下.. 发现..居然不一样... 找了模板题试了试..百度的不好用 反正就是wa了..果然还是应当跟着学长混.. 图两边的点分 ...
- hdu 1711 KMP算法模板题
题意:给你两个串,问你第二个串是从第一个串的什么位置開始全然匹配的? kmp裸题,复杂度O(n+m). 当一个字符串以0为起始下标时.next[i]能够描写叙述为"不为自身的最大首尾反复子串 ...
- Sliding Window POJ - 2823 单调队列模板题
Sliding Window POJ - 2823 单调队列模板题 题意 给出一个数列 并且给出一个数m 问每个连续的m中的最小\最大值是多少,并输出 思路 使用单调队列来写,拿最小值来举例 要求区间 ...
- poj 1274 The Perfect Stall【匈牙利算法模板题】
The Perfect Stall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 20874 Accepted: 942 ...
- POJ 1459 Power Network(网络最大流,dinic算法模板题)
题意:给出n,np,nc,m,n为节点数,np为发电站数,nc为用电厂数,m为边的个数. 接下来给出m个数据(u,v)z,表示w(u,v)允许传输的最大电力为z:np个数据(u)z,表示发电 ...
- Bellman-Ford算法模板题
POJ 3259 虫洞(Bellman-Ford判断有无负环的问题) 描述: 在探索他的许多农场时,Farmer John发现了许多令人惊叹的虫洞.虫洞是非常奇特的,因为它是一条单向路径,在您进入虫洞 ...
- 图论之最短路径floyd算法
Floyd算法是图论中经典的多源最短路径算法,即求任意两点之间的最短路径. 它可采用动态规划思想,因为它满足最优子结构性质,即最短路径序列的子序列也是最短路径. 举例说明最优子结构性质,上图中1号到5 ...
- POJ1125-Stockbroker Grapevine【Floyd】(模板题)
<题目链接> 题目大意: 题目可能有多组测试数据,每个测试数据的第一行为经纪人数量N(当N=0时,输入数据结束),然后接下来N行描述第i(1<=i<=N)个经纪人与其他经纪人的 ...
- POJ 3348 Cows | 凸包模板题
题目: 给几个点,用绳子圈出最大的面积养牛,输出最大面积/50 题解: Graham凸包算法的模板题 下面给出做法 1.选出x坐标最小(相同情况y最小)的点作为极点(显然他一定在凸包上) 2.其他点进 ...
随机推荐
- jQuery属性操作之.val()函数
目录 .val()实例方法的三种用法 .val()函数源码 调用形式:$('xxx').val(); 调用形式:$('xxx').val(value); 调用形式:$('xxx').val(funct ...
- golang总结-并发
目录 2.7 并发编程 go协程 go管道 2.7 并发编程 go协程 golang 通过一个go关键字就可以开启一个协程. func main() { //两个交错输出 go sayHello() ...
- SQL逻辑查询处理的步骤序号
(8)SELECT (9) DISTINCT <select_list> (1)FROM <left_table> (3)<join_type>JOIN<ri ...
- thinkphp+redis实现秒杀,缓存等功能
秒杀是商城常见功能 php+redis是最常见的秒杀功能 1,安装redis,根据自己的php版本安装对应的redis扩展 首先查看phpinfo();php环境信息 2,下载redis https ...
- Python - 入门基础(一)
1.解释器路径 #!/usr/bin/env python 2.编码 # -*- coding:utf8 -*- 1.ascill ---00000000 (8个位表示) 缺点:表示不了英文 2.u ...
- SylixOS 系统初探
国产嵌入式硬实时操作系统 SylixOS 初体验 关于 SylixOS 详细了解请见:http://wiki.sylixos.com/index.php/%E7%B3%BB%E7%BB%9F%E7%A ...
- Python学习:15.Python面向对象(二、继承的各种情况)
一.什么是继承 继承是一种创建类的方法,在python中,一个类可以继承来自一个或多个父.原始类称为基类或超类. #创建父类 class Parent1: pass class Parent2: pa ...
- gcd 模板
声明 给 x,y 两个数,求 x,y 的最大公因数. 辗转相除法,直接套!!! function gcd(x,y:longint):longint; begin then exit(x) else e ...
- 20155235 2006-2007-2 《Java程序设计》第1周学习总结
20155235 2006-2007-2 <Java程序设计>第1周学习总结 教材学习内容总结 第二章 使用的JRE不同,对JAVA的执行有什么影响 第三章 字符串的用法在JAVA和C中有 ...
- gitlab在push代码的时候报错
一.问题报错 gitlab在执行git pull origin master,拉取代码的时候报如下错误. $ git pull origin master remote: Counting objec ...