# 牛棚安排

### 题目大意：

$n$头牛和$m$个牛棚，每头牛有自己第1喜欢，第2喜欢……第$m$喜欢的牛棚（开心度分别为$m,m-1,m-2……1$），然后读入$m$个数表示第$i$个牛棚最多可以进多少头牛，现在让所有牛都进入到牛棚内，问最开心的牛和最伤心的牛开心度之差最小是多少

### 输入样例

6 4
1 2 3 4
2 3 1 4
4 2 3 1
3 1 2 4
1 3 4 2
1 4 2 3
2 1 3 2


### 输出样例

2


### 代码

#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;
int n, m, l, r, mid, sum, s, t, tot, b[50], head[2000], dep[2000], v[2100][50];

const int inf = 1 << 29;

struct rec
{
int to, next, edge;
}a[40500];

queue<int> d;

void add(int x, int y, int z)
{
a[++tot].to = y;
a[tot].edge = z;

a[++tot].to = x;
a[tot].edge = 0;
}

bool bfs()
{
memset(dep, 0, sizeof(dep));
while(!d.empty())	d.pop();
d.push(s);
dep[s] = 1;
while (!d.empty())
{
int h = d.front();
d.pop();
for (int i = head[h]; i; i = a[i].next)
if (!dep[a[i].to] && a[i].edge)
{
dep[a[i].to] = dep[h] + 1;
if (a[i].to == t) return true;
d.push(a[i].to);
}
}
return false;
}

int dinic(int x, int flow)
{
if (x == t) return flow;
int rest = 0, k;
for (int i = head[x]; i; i = a[i].next)
if (dep[x] + 1 == dep[a[i].to] && a[i].edge)
{
k = dinic(a[i].to, min(a[i].edge, flow - rest));
if (!k) dep[a[i].to] = 0;
rest += k;
a[i].edge -= k;
a[i^1].edge += k;
if (rest == flow) return rest;
}
return rest;
}

bool js(int x)
{
s = 0;
t = n + m + 1;
for (int i = 1; i <= m - x + 1; ++i)//开头位置
{
tot = 0;
for (int j = 1; j <= m; ++j)
for (int j = 1; j <= n; ++j)
{
for (int k = i; k - i + 1 <= x; ++k)
}
sum = 0;
while(bfs())//找增广路
sum += dinic(s, inf);//流
if (sum == n) return true;//可以满足
}
return false;
}
int main()
{
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j)
scanf("%d", &v[i][j]);//先存下来，到时再连边
for (int i = 1; i <= m; ++i)
scanf("%d", &b[i]);
l = 1;
r = m;
while(l < r)//二分枚举
{
mid = (l + r) >> 1;
if (js(mid)) r = mid;
else l = mid + 1;
}
printf("%d", l);
return 0;
}


©️2019 CSDN 皮肤主题: 精致技术 设计师: CSDN官方博客