图论---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.其他点进 ...
随机推荐
- cookie,session,token
发展史 1.很久很久以前,Web 基本上就是文档的浏览而已, 既然是浏览,作为服务器, 不需要记录谁在某一段时间里都浏览了什么文档,每次请求都是一个新的HTTP协议, 就是请求加响应, 尤其是我不用 ...
- JavaScript入门学习(1)
<html> <script type ="text/javascript"> var i,j; for (i=1;i<10;i++){ for (j ...
- docker 容器 设置网络代理
以/bin/bash 形式进入容器: [设置http 及https代理],如下: export http_proxy=http://172.16.0.20:3128 export https_prox ...
- Nexus Repository3安装和maven,npm配置(Linux)
Nexus Repository下载 根据操作系统选择指定版本,本文针对Linux安装,其他的安装过程可能有所差异. https://help.sonatype.com/repomanager3/do ...
- Linux - 时间相关命令 - ntpdate, date, hwclock
1. 概述 最近也不知道写啥了, 把之前的老文档整理一下, 凑个数什么的 配置时间这种工作, 偶尔还是要用一下 主要描述 3 个命令的简单适用 ntpdate hwlock 2. ntpdate 1. ...
- 20155216 实验一《Java开发环境的熟悉》实验报告
实验内容 1.使用JDK编译.运行简单的Java程序. 2.使用idea 编辑.编译.运行.调试Java程序. 3.实现四则运算,并进行测试. 4.实现带有"()"的"+ ...
- # 20155224 2016-2017-2 《Java程序设计》第10周学习总结
20155224 2016-2017-2 <Java程序设计>第10周学习总结 教材学习内容总结 密码学: 主要是研究保密通信和信息保密的学科, 包括信息保密传输和信息加密存储等. 密码学 ...
- 一篇文章帮你梳理清楚API设计时需要考虑的几个关键点
本文作者是Enchant的架构师,他最近研究了Netflix.SoundCloud.谷歌.亚马逊.Spotify等公司的微服务实践,并根据自己的理解总结出了一套适用于现代Web和云技术的微服务实战经验 ...
- html模板 练习(仿照抽屉网)
1.页面布局 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UT ...
- SSM-CRUD实战
前端最容易出现缓存问题,所以以后每次都必须完全在idea加载完后,再在浏览器端多 执行 ctrl+F5 索要最新copy 这样就能拿到最新的改动了,就不会出现各种代码没问题但是功能就是实现不了的问题 ...