【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 ...
随机推荐
- ORA-01033: ORACLE initialization or shutdown in progress问题
这是Oracle12c中笔者遇到的一个错误提示:ORA-01033: ORACLE initialization or shutdown in progress 错误的中文意思是:Oracle初始化未 ...
- Executors
提供了工厂方法: Factory and utility methods for Executor, ExecutorService, ScheduledExecutorService, Thread ...
- 2015多校联合训练赛 Training Contest 4 1008
构造题: 比赛的时候只想到:前面一样的数,后面 是类似1,2,3,4,5,6....t这 既是:t+1,t+1...,1,2,3,...t t+1的数目 可能 很多, 题解时YY出一个N 然后对N ...
- Ubuntu 16.04下更新Atom
在Ubuntu下Atom好像不会自动更新,但是可以通过这些方法去实现: 1.安装插件:https://atom.io/packages/up2date 2.使用apt源更新: sudo apt-get ...
- Maven的仓库
以下内容引用自https://ayayui.gitbooks.io/tutorialspoint-maven/content/book/maven_repositories.html: 什么是Mave ...
- Protostuff序列化和反序列化使用说明
原文:http://blog.csdn.net/zhglance/article/details/56017926 google原生的protobuffer使用起来相当麻烦,首先要写.proto文件, ...
- 【kotlin】报错 Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type List<String>?
报错如下: 解决如下: 另一种情况: 解决如下:
- Linux上利用NFS实现远程挂载
两台服务器 192.168.1.1 [WEB服务器] 192.168.1.2 [录音服务器] 说明 在192.168.1.2服务器上每天有许多录音文件生成,192.168.1.1作为WEB服务器,里面 ...
- 空暇时候思考2('\0'等价于数字0还是字符0)
/********************************************************************** * * Copyright (c)2015,WK Stu ...
- Coursera 机器学习Course Wiki Lecture Notes
https://share.coursera.org/wiki/index.php/ML:Main 包含了每周的Lecture Notes,以便复习回顾的时候使用.