【USACO 2857】 Steady Cow Assignment
【题目链接】
【算法】
二分答案,check的时候跑最大流,即可
【代码】
#include<bits/stdc++.h>
using namespace std;
#define MAXN 2000
#define MAXB 1000 int i,j,low,high,mid,st,ed,N,B,tot,ans,tmp;
int h[MAXN+MAXB],U[MAXN*MAXB],V[MAXN*MAXB],
W[MAXN*MAXB],Head[MAXN*MAXB],Next[MAXN*MAXB],
other[MAXN*MAXB],v[MAXB+],a[MAXN+][MAXB+]; template <typename T> void read(T &x) {
int f=; char c = getchar(); x=;
for (; !isdigit(c); c = getchar()) { if (c=='-') f = -f; }
for (; isdigit(c); c = getchar()) x=x*+c-'';
x*=f;
}
template <typename T> inline void write(T x) {
if (x < ) { putchar('-'); x = -x; }
if (x > ) write(x/);
putchar(x % + '');
} template <typename T> inline void writeln(T x) {
write(x);
puts("");
} inline void add(int a,int b,int c) {
++tot;
U[tot] = a; V[tot] = b; W[tot] = c;
Next[tot] = Head[a]; Head[a] = tot;
other[tot] = ++tot;
U[tot] = b; V[tot] = a; W[tot] = ;
Next[tot] = Head[b]; Head[b] = tot;
other[tot] = tot - ;
} inline bool bfs() {
int i,x,y;
queue<int> q;
memset(h,,sizeof(h));
h[st] = ; q.push(st);
while (!q.empty()) {
x = q.front(); q.pop();
for (i = Head[x]; i; i = Next[i]) {
y = V[i];
if ((W[i] > ) && (!h[y])) {
h[y] = h[x] + ;
q.push(y);
}
}
}
if (h[ed]) return true;
else return false;
} inline int maxflow(int x,int f) {
int i,t,y,sum=;
if (x == ed) return f;
for (i = Head[x]; i; i = Next[i]) {
y = V[i];
if ((W[i] > ) && (h[y] == h[x] + ) && (sum < f)) {
sum += (t = maxflow(y,min(W[i],f-sum)));
W[i] -= t; W[other[i]] += t;
}
}
if (!sum) h[x] = ;
return sum;
} inline bool check(int ml) {
int i,j,l,r,sum;
for (l = ; l <= N - ml + ; l++) {
r = l + ml - ;
for (i = ; i <= tot; i++) Head[i] = ;
tot = ;
for (i = ; i <= N; i++) add(st,i,);
for (i = ; i <= B; i++) add(i+N,ed,v[i]);
for (i = ; i <= N; i++) {
for (j = ; j <= B; j++) {
if ((a[i][j] >= l) && (a[i][j] <= r))
add(i,j+N,);
}
}
sum = ;
while (bfs()) {
sum += maxflow(st,N);
}
if (sum == N) return true;
}
return false;
} int main() { read(N); read(B);
st = N + B + ; ed = st + ; for (i = ; i <= N; i++) {
for (j = ; j <= B; j++) {
read(tmp);
a[i][tmp] = j;
}
}
for (i = ; i <= B; i++) read(v[i]); low = ; high = B;
while (low <= high) {
mid = (low + high) / ;
if (check(mid)) {
high = mid - ;
ans = mid;
} else
low = mid + ;
}
writeln(ans); return ; }
【USACO 2857】 Steady Cow Assignment的更多相关文章
- POJ 3189——Steady Cow Assignment——————【多重匹配、二分枚举区间长度】
Steady Cow Assignment Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I ...
- POJ3189 Steady Cow Assignment
Steady Cow Assignment Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6817 Accepted: ...
- POJ3189 Steady Cow Assignment —— 二分图多重匹配/最大流 + 二分
题目链接:https://vjudge.net/problem/POJ-3189 Steady Cow Assignment Time Limit: 1000MS Memory Limit: 65 ...
- POJ3189:Steady Cow Assignment(二分+二分图多重匹配)
Steady Cow Assignment Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7482 Accepted: ...
- Poj 3189 Steady Cow Assignment (多重匹配)
题目链接: Poj 3189 Steady Cow Assignment 题目描述: 有n头奶牛,m个棚,每个奶牛对每个棚都有一个喜爱程度.当然啦,棚子也是有脾气的,并不是奶牛想住进来就住进来,超出棚 ...
- 1434:【例题2】Best Cow Fences
1434:[例题2]Best Cow Fences 时间限制: 1000 ms 内存限制: 65536 KB提交数: 263 通过数: 146 [题目描述] 给定一个长度为n的 ...
- 【USACO 2017FEB】 Why Did the Cow Cross the Road III
[题目链接] 点击打开链接 [算法] 树状数组 [代码] #include<bits/stdc++.h> using namespace std; #define MAXN 100010 ...
- 【USACO 2017Feb】 Why Did the Cow Cross the Road
[题目链接] 点击打开链接 [算法] dist[i][j][k]表示当前走到(i,j),走的步数除以3的余数为k的最小花费 spfa即可 [代码] #include<bits/stdc++.h& ...
- 【USACO 2011】 道路和航线
[题目链接] 点击打开链接 [算法] SPFA + SLF / LLL 优化 [代码] #include<bits/stdc++.h> using namespace std; #defi ...
随机推荐
- codeforces Gym 101572 I 有向图最小环路径
题目链接 http://codeforces.com/gym/101572 题意 一共n个文件 存在依赖关系 根据给出的依赖关系 判断是否存在循环依赖 ,不存在的话输出SHIP IT,存在的话 ...
- [bzoj1110][POI2007]砝码Odw_贪心
bzoj-1110 POI-2007 砝码Odw 参考博客:http://hzwer.com/4761.html 题目大意:在byteotian公司搬家的时候,他们发现他们的大量的精密砝码的搬运是一件 ...
- P1717 钓鱼 洛谷
https://www.luogu.org/problem/show?pid=1717 题目描述 话说发源于小朋友精心设计的游戏被电脑组的童鞋们藐杀之后非常不爽,为了表示安慰和鼓励,VIP999决定请 ...
- P2007 魔方
洛谷——P2007 魔方 题目背景 常神牛从来没接触过魔方,所以他要借助计算机来玩.即使是这样,他还是很菜. 题目描述 常神牛家的魔方都是3*3*3的三阶魔方,大家都见过. (更正:3 4以图为准.) ...
- hadoop+yarn+hbase+storm+kafka+spark+zookeeper)高可用集群详细配置
配置 hadoop+yarn+hbase+storm+kafka+spark+zookeeper 高可用集群,同时安装相关组建:JDK,MySQL,Hive,Flume 文章目录 环境介绍 节点介绍 ...
- weex 项目开发(五)自定义 过滤函数 和 混合 及 自定义 Header 组件
1.自定义 过滤函数 src / filters / index.js /** * 自定义 过滤函数 */ export function host (url) { if (!url) return ...
- 生成可重集的排序 (白书P184)
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> us ...
- overflow(超出部分省略号)
溢出:overflow:visible/hidden/scroll/auto/inherit: visible:默认值.不剪切.hidden:超出部分剪切.没有滚动条.scroll:超出部分有滚动条. ...
- uva 11468 - Substring(AC自己主动机+概率)
题目链接:uva 11468 - Substring 题目大意:给出一些字符和各自字符相应的选择概率.随机选择L次后得到一个长度为L的字符串,要求该字符串不包括随意一个子串的概率. 解题思路:构造AC ...
- 第8章4节《MonkeyRunner源代码剖析》MonkeyRunner启动执行过程-启动AndroidDebugBridge
上一节我们看到在启动AndroidDebugBridge的过程中会调用其start方法,而该方法会做2个基本的事情: 715行startAdb:开启AndroidDebugBridge 722-723 ...