Ranking the Cows
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 3086 | Accepted: 1438 |
Description
Each of Farmer John's N cows (1 ≤ N ≤ 1,000) produces milk at a different positive rate, and FJ would like to order his cows according to these rates from the fastest milk producer to the slowest.
FJ has already compared the milk output rate for M (1 ≤ M ≤ 10,000) pairs of cows. He wants to make a list ofC additional pairs of cows such that, if he now compares those C pairs, he will definitely be able to deduce the correct ordering of allN cows. Please help him determine the minimum value of C for which such a list is possible.
Input
Line 1: Two space-separated integers:
N and
M
Lines 2.. M+1: Two space-separated integers, respectively: X and Y. Both X and Y are in the range 1... N and describe a comparison where cow X was ranked higher than cow Y.
Lines 2.. M+1: Two space-separated integers, respectively: X and Y. Both X and Y are in the range 1... N and describe a comparison where cow X was ranked higher than cow Y.
Output
Line 1: A single integer that is the minimum value of
C.
Sample Input
5 5 2 1 1 5 2 3 1 4 3 4
Sample Output
3
Hint
From the information in the 5 test results, Farmer John knows that since cow 2 > cow 1 > cow 5 and cow 2 > cow 3 > cow 4, cow 2 has the highest rank. However, he needs to know whether cow 1 > cow 3 to determine the cow with the second highest rank. Also, he will need one more question to determine the ordering between cow 4 and cow 5. After that, he will need to know if cow 5 > cow 3 if cow 1 has higher rank than cow 3. He will have to ask three questions in order to be sure he has the rankings: "Is cow 1 > cow 3? Is cow 4 > cow 5? Is cow 5 > cow 3?"
题目大意:
农夫约翰有N(1≤N≤1000)头奶牛,每一头奶牛都有一个确定的独一无二的正整数产奶率.约翰想要让这些奶牛按产奶率从高到低排序. 约翰已经比较了M(1≤M≤10000)对奶牛的产奶率,但他发现,他还需要再做一张关于另外C对奶牛的产奶率比较,才能推断出所有奶牛的产奶率排序.请帮他确定C的最小值.
输入:
第1行包含两个用空格分开的整数N和M.接下来M行,每行有两个用空格分开的整数X和Y(1≤X,y≤1000),表示奶牛X的产奶率高于奶牛Y.
输出:
C的最小值。
思路:bitset优化Floyd
详情:http://blog.csdn.net/wzw1376124061/article/details/69870161
题目大意:
农夫约翰有N(1≤N≤1000)头奶牛,每一头奶牛都有一个确定的独一无二的正整数产奶率.约翰想要让这些奶牛按产奶率从高到低排序. 约翰已经比较了M(1≤M≤10000)对奶牛的产奶率,但他发现,他还需要再做一张关于另外C对奶牛的产奶率比较,才能推断出所有奶牛的产奶率排序.请帮他确定C的最小值.
输入:
第1行包含两个用空格分开的整数N和M.接下来M行,每行有两个用空格分开的整数X和Y(1≤X,y≤1000),表示奶牛X的产奶率高于奶牛Y.
输出:
C的最小值。
思路:bitset优化Floyd
详情:http://blog.csdn.net/wzw1376124061/article/details/69870161
代码:
/*************************************************************************
> Author: wzw-cnyali
> Created Time: 2017/8/29 15:48:29
************************************************************************/
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<bitset>
#prag\
ma GCC optimize("O3")
using namespace std;
typedef long long LL;
typedef unsigned long long uLL;
#define REP(i, a, b) for(register int i = (a), i##_end_ = (b); i <= i##_end_; ++ i)
#define DREP(i, a, b) for(register int i = (a), i##_end_ = (b); i >= i##_end_; -- i)
#define EREP(i, a) for(register int i = (be[a]); i != -1; i = nxt[i])
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define mem(a, b) memset((a), b, sizeof(a))
template<typename T> inline bool chkmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; }
template<typename T> inline bool chkmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; }
char buff[1 << 25], *buf = buff;
template <class T>
T read(T sum = 0, T fg = 0)
{
while(*buf < '0' || *buf > '9') { fg |= *buf == '-'; buf++; }
while(*buf >= '0' && *buf <= '9') { sum = sum * 10 + *buf - '0'; buf++; }
return fg ? -sum : sum;
}
const int Size = 1010;
bitset <Size> MAP[Size];
void floyd(int n)
{
REP(i, 1, n) REP(j, 1, n)
if(MAP[j][i]) MAP[j] |= MAP[i];
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.in", "r", stdin);
freopen("output.out", "w", stdout);
#endif
fread(buff, 1, 1 << 25, stdin);
int n = read<int>(), m = read<int>();
REP(i, 1, m)
{
int x = read<int>(), y = read<int>();
MAP[x][y] = 1;
}
floyd(n);
int ans = 0;
REP(i, 1, n) REP(j, i + 1, n)
if(!MAP[i][j] && !MAP[j][i]) ans++;
printf("%d\n", ans);
return 0;
}
本文介绍了一种使用 bitset 优化 Floyd 算法的方法,用于解决农夫约翰需要通过最少次数的比较来完全确定其 N 头奶牛的产奶率排名的问题。通过输入已知的奶牛产奶率对比数据,该算法能有效找出还需要进行多少次比较才能确保所有奶牛正确排序。

485

被折叠的 条评论
为什么被折叠?



