【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 ...
随机推荐
- spring data jpa 查询部分字段
@Query("select new map(ah as ah,salq as sqlq,yg as yg, bg as bg,ay as ay) FROM Aj where ahdm=?1 ...
- spring boot 添加mybatis,以及相关配置
首先在pom.xml文件里加入 <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifa ...
- Tomcat服务器解析“GET /JavaWebDemo1/1.jsp HTTP/1.1”
(2)服务器收到http请求报文,返回http响应报文 Tomcat服务器解析“GET /JavaWebDemo1/1.jsp HTTP/1.1” Tomcat服务器解析“GET /JavaWebDe ...
- uicollectionview 使用uibutton或者uiimageview实现旋转出现scale的问题
uicollectionview下单独使用uibutton然后setimage或者直接使用uiimageview然后一定角度旋转后发现size会变动 解决方案:添加uibutton到uicollect ...
- iOS release版本去除NSLog打印信息
因为NSLog的输出还是比较消耗系统资源的,而且输出的数据也可能会暴露出App里的保密数据,所以发布正式版时需要把这些输出全部屏蔽掉. 我们可以在发布版本前先把所有NSLog语句注释掉,等以后要调试时 ...
- 汉澳sinox通过ndis执行windows驱动程序
汉澳sinox不仅能通过wine执行windows应用程序.还能通过ndis执行windows驱动程序 汉澳sinox使用 Windows NDIS 驱动程序 详细实现方法是用ndisgen把wind ...
- JNI基础知识
JNI是在学习Android HAL时必须要面临一个知识点,假设你不了解它的机制,不了解它的使用方式,你会被本地代码绕的晕头转向,JNI作为一个中间语言的翻译官在运行Java代码的Android中有着 ...
- insserv: warning: script 'lampp' missing LSB tags and overrides
https://ubuntuforums.org/showthread.php?t=2327011 1.方法一,编辑rc.loacl脚本 Ubuntu开机之后会执行/etc/rc.local文件中的脚 ...
- add swapspace file on ubuntu.
https://askubuntu.com/questions/566745/allocate-swap-after-ubuntu-14-04-lts-installation ----------- ...
- 【Objective-C】09-空指针和野指针
一.什么是空指针和野指针 1.空指针 1> 没有存储不论什么内存地址的指针就称为空指针(NULL指针) 2> 空指针就是被赋值为0的指针.在没有被详细初始化之前.其值为0. 以下两个都是空 ...