POJ3275 Ranking the Cows floyd的bitset优化

POJ3275 Ranking the Cows

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <bitset>
 4 using namespace std;
 5 const int maxn = 1005;
 6 int n, m;
 7 bitset<maxn> maps[maxn];
 8 void floyd() {
 9     for (int k = 1; k <= n; k++) {
10         for (int i = 1; i <= n; i++) {
11             if (maps[i][k]) maps[i] |= maps[k];
12         }
13     }
14 }
15 int main() {
16     scanf("%d%d",&n,&m);
17     for (int i = 1; i <= m; i++) {
18         int u, v; scanf("%d%d",&u,&v);
19         maps[u][v] = true;
20     }
21     floyd();
22     int ans = 0;
23     for (int i = 1; i <= n; i++) {
24         for (int j = i+1; j <= n; j++) {
25             if (!maps[i][j] && !maps[j][i]) ans++;
26         }
27     }
28     printf("%d\n",ans);
29     return 0;
30 }

原文地址:https://www.cnblogs.com/wstong/p/11745357.html

时间: 2024-10-13 15:40:22

POJ3275 Ranking the Cows floyd的bitset优化的相关文章

BZOJ 1703 [Usaco2007 Mar]Ranking the Cows 奶牛排名 bitset优化

题意:链接 方法: bitset传递闭包 解析: 显然答案为无序点对的个数. 但是无序点对的个数怎么求呢? 容斥原理. 所有点对个数减去有序点对的个数即为答案. 怎么维护有序点对个数呢? bitset传递闭包即可. 人生中的第一次rnk1!!!!!!!!!!! 代码: #include <bitset> #include <cstdio> #include <cstring> #include <iostream> #include <algorith

POJ3275:Ranking the Cows(Bitset加速floyd求闭包传递)

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

POJ-3275:Ranking the Cows(Floyd、bitset)

Ranking the Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 3301   Accepted: 1511 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

Bzoj 1703: [Usaco2007 Mar]Ranking the Cows 奶牛排名 传递闭包,bitset

1703: [Usaco2007 Mar]Ranking the Cows 奶牛排名 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 323  Solved: 238[Submit][Status][Discuss] Description 农夫约翰有N(1≤N≤1000)头奶牛,每一头奶牛都有一个确定的独一无二的正整数产奶率.约翰想要让这些奶牛按产奶率从高到低排序.    约翰已经比较了M(1≤M≤10000)对奶牛的产奶率,但他发现,他还需要再做一

hdu 5036 Explosion bitset优化floyd

http://acm.hdu.edu.cn/showproblem.php?pid=5036 题意就是给定一副有向图,现在需要走遍这n个顶点,一开始出发的顶点是这n个之中的随便一个. 如果走了1,那么1能联通的顶点就可以直接走过去,其他不和1连通的,就需要炸坏.问需要炸弹的期望. 比如一副图是1-->2-->3的.那么期望是11 / 6 假如从1号点开始,1/3概率选中1号点开始,那么需要炸弹数是1(炸开一号),贡献是1/3 假如从2号点开始,1/3概率选中2号点开始,那么需要炸开2号点,然后

【BZOJ】1703: [Usaco2007 Mar]Ranking the Cows 奶牛排名

[题意]给定n头牛和m对大小关系,求最坏情况下至少还需要比较几对奶牛的大小(在未确定顺序的奶牛对中随机比较) [算法]floyd求传递闭包 [题解]可达说明大小已知,则不可达点对数量就是最少比较次数. 使用bitset优化传递闭包,复杂度O(n^3 /32). #include<cstdio> #include<bitset> #include<algorithm> using namespace std; const int maxn=1010; int n,m; b

hdu 5036 Explosion (bitset优化的传递闭包求解概率)

Explosion Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Submission(s): 142    Accepted Submission(s): 25 Problem Description Everyone knows Matt enjoys playing games very much. Now, he is playing such a g

P2881 [USACO07MAR]排名的牛Ranking the Cows

bitset优化传递闭包模板题 这种关系直接用图论来建模就是了,其实就是一个传递闭包. 传递闭包有一个朴素的做法就是floyd. 而这道题的范围是\(n \leq 1000\),\(n^3\)的暴力显然会T. 而使用bitset,听说可以优化到原做法的\(\frac{1}{32}\)甚至更好! 直接给代码其实是自己不懂原理 #include<cstdio> #include<bitset> const int maxn = 1005; std::bitset<maxn>

Codeforces 788C The Great Mixing(背包问题建模+bitset优化或BFS)

[题目链接] http://codeforces.com/problemset/problem/788/C [题目大意] 给出一些浓度的饮料,要求调出n/1000浓度的饮料,问最少需要多少升饮料 [题解] 设浓度为a,现在要求出系数x1,x2,x3……,使得x1*a1+x2*a2+x3*a3+……=n*(x1+x2+x3+……) 得a1*(x1-n)+a2*(x2-n)+a3*(x3-n)+……=0 假设现在有x1-n和x2-n,设其数值为x和y,那么一定有(x)*y+(-y)*x=0, x+y