POJ-3189-Steady Cow Assignment(最大流+枚举)
题意
此题题意不太好懂。现有n头牛和b个牛棚,每个牛棚可以养的牛的数目都有一个限制c[i],表示该牛棚最多只能关c[i]头牛,每头牛对每一个牛棚都有一个喜爱值,用1到b来表示,现在要安排这些牛,使得牛棚中的牛对牛棚的最大喜爱值与最小喜爱值的差值最小.
分析:
枚举区间+最大流
新建源点向每头牛连边
牛向barns连边
barns向汇点连边,边权为barns的容量,其实就是一个二分图
最后求最大流就好了
// File Name: 3189.cpp
// Author: Zlbing
// Created Time: 2013年08月15日 星期四 16时26分07秒 #include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<set>
#include<map>
#include<vector>
#include<cstring>
#include<stack>
#include<cmath>
#include<queue>
using namespace std;
#define CL(x,v); memset(x,v,sizeof(x));
#define INF 0x3f3f3f3f
#define LL long long
#define REP(i,r,n) for(int i=r;i<=n;i++)
#define RREP(i,n,r) for(int i=n;i>=r;i--)
const int MAXN=;
struct Edge{
int from,to,cap,flow;
};
bool cmp(const Edge& a,const Edge& b){
return a.from < b.from || (a.from == b.from && a.to < b.to);
}
struct Dinic{
int n,m,s,t;
vector<Edge> edges;
vector<int> G[MAXN];
bool vis[MAXN];
int d[MAXN];
int cur[MAXN];
void init(int n){
this->n=n;
for(int i=;i<=n;i++)G[i].clear();
edges.clear();
}
void AddEdge(int from,int to,int cap){
edges.push_back((Edge){from,to,cap,});
edges.push_back((Edge){to,from,,});//当是无向图时,反向边容量也是cap,有向边时,反向边容量是0
m=edges.size();
G[from].push_back(m-);
G[to].push_back(m-);
}
bool BFS(){
CL(vis,);
queue<int> Q;
Q.push(s);
d[s]=;
vis[s]=;
while(!Q.empty()){
int x=Q.front();
Q.pop();
for(int i=;i<G[x].size();i++){
Edge& e=edges[G[x][i]];
if(!vis[e.to]&&e.cap>e.flow){
vis[e.to]=;
d[e.to]=d[x]+;
Q.push(e.to);
}
}
}
return vis[t];
}
int DFS(int x,int a){
if(x==t||a==)return a;
int flow=,f;
for(int& i=cur[x];i<G[x].size();i++){
Edge& e=edges[G[x][i]];
if(d[x]+==d[e.to]&&(f=DFS(e.to,min(a,e.cap-e.flow)))>){
e.flow+=f;
edges[G[x][i]^].flow-=f;
flow+=f;
a-=f;
if(a==)break;
}
}
return flow;
}
//当所求流量大于need时就退出,降低时间
int Maxflow(int s,int t,int need){
this->s=s;this->t=t;
int flow=;
while(BFS()){
CL(cur,);
flow+=DFS(s,INF);
if(flow>need)return flow;
}
return flow;
}
//最小割割边
vector<int> Mincut(){
BFS();
vector<int> ans;
for(int i=;i<edges.size();i++){
Edge& e=edges[i];
if(vis[e.from]&&!vis[e.to]&&e.cap>)ans.push_back(i);
}
return ans;
}
void Reduce(){
for(int i = ; i < edges.size(); i++) edges[i].cap -= edges[i].flow;
}
void ClearFlow(){
for(int i = ; i < edges.size(); i++) edges[i].flow = ;
}
};
Dinic solver;
int g[MAXN][];
int cap[];
int n,m;
int build(int l,int r)
{
solver.init(n+m+);
int s=n+m;
int t=n+m+;
for(int i=;i<n;i++)
solver.AddEdge(s,i,);
for(int i=;i<m;i++)
solver.AddEdge(i+n,t,cap[i]);
for(int i=;i<n;i++)
for(int j=l;j<r;j++)
{
solver.AddEdge(i,g[i][j]-+n,);
}
int maxflow=solver.Maxflow(s,t,INF);
return maxflow;
}
int solve()
{
for(int i=;i<=m;i++)
for(int j=;j+i-<m;j++)
{
int ret= build(j,j+i);
if(ret==n)
return i;
} }
int main()
{
while(~scanf("%d%d",&n,&m))
{
REP(i,,n-)
REP(j,,m-)
{
scanf("%d",&g[i][j]);
}
REP(i,,m-)
scanf("%d",&cap[i]);
int ans=solve();
printf("%d\n",ans);
}
return ;
}
POJ-3189-Steady Cow Assignment(最大流+枚举)的更多相关文章
- Poj 3189 Steady Cow Assignment (多重匹配)
题目链接: Poj 3189 Steady Cow Assignment 题目描述: 有n头奶牛,m个棚,每个奶牛对每个棚都有一个喜爱程度.当然啦,棚子也是有脾气的,并不是奶牛想住进来就住进来,超出棚 ...
- POJ 3189——Steady Cow Assignment——————【多重匹配、二分枚举区间长度】
Steady Cow Assignment Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I ...
- POJ 3189 Steady Cow Assignment
题意:每个奶牛对所有的牛棚有个排名(根据喜欢程度排的),每个牛棚能够入住的牛的数量有个上限,重新给牛分配牛棚,使牛棚在牛心中的排名差(所有牛中最大排名和最小排名之差)最小. 题目输入: 首先是两个 ...
- POJ 3189 Steady Cow Assignment【网络流】
题意:每个奶牛对所有的牛棚有个排名(根据喜欢程度排的),每个牛棚能够入住的牛的数量有个上限,重新给牛分配牛棚,使牛棚在牛心中的排名差(所有牛中最大排名和最小排名之差)最小. 牛棚个数最多为20,那么直 ...
- POJ 3189 Steady Cow Assignment 【二分】+【多重匹配】
<题目链接> 题目大意: 有n头牛,m个牛棚,每个牛棚都有一定的容量(就是最多能装多少只牛),然后每只牛对每个牛棚的喜好度不同(就是所有牛圈在每个牛心中都有一个排名),然后要求所有的牛都进 ...
- 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: ...
- POJ3189 Steady Cow Assignment
Steady Cow Assignment Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6817 Accepted: ...
- POJ 2289 Jamie's Contact Groups & POJ3189 Steady Cow Assignment
这两道题目都是多重二分匹配+枚举的做法,或者可以用网络流,实际上二分匹配也就实质是网络流,通过枚举区间,然后建立相应的图,判断该区间是否符合要求,并进一步缩小范围,直到求出解.不同之处在对是否满足条件 ...
- Steady Cow Assignment POJ - 3189 (最大流+匹配)
Farmer John's N (1 <= N <= 1000) cows each reside in one of B (1 <= B <= 20) barns which ...
随机推荐
- ArrayBlockingQueue 源码阅读 问题(一)
今天阅读java.util.concurrent 中 ArrayBlockingQueue 的源码,发现其中有很多下面这行代码 final ReentrantLock lock = this.lock ...
- css 嵌套 元素所属类别
元素所属类别 Metadata content(元数据元素)(8) base,link,meta,noscript,script,style,template, title Flow content( ...
- 自己写的自动生成动态边框的jquery小插件
思路就是在元素四周添加<ul>列表,然后周期性地改变它的颜色,实现动态的效果,不支持ie7.ie8 预览链接http://gorey.sinaapp.com/myBorder/border ...
- oracle中用comment on的用法
oracle中用comment on命令给表或字段加以说明,语法如下:COMMENT ON { TABLE [ schema. ] { table | view } | COLUMN [ s ...
- C#中byte[]与string的转换
1. System.Text.UnicodeEncoding converter = new System.Text.UnicodeEncoding(); byte[] i ...
- 替换 wcf 消息传输中的 命名空间
替换 wcf 消息传输中的 命名空间,http://vanacosmin.ro/Articles/Read/WCFEnvelopeNamespacePrefix
- Hadoop的读写类调用关系_图示
- How to Make LastPass Even More Secure with Google Authenticator
Google Authenticator LastPass supports Google Authenticator, which is officially available as an app ...
- 十七、C# 反射、特性和动态编程
反射.特性和动态编程 1.访问元数据 2.成员调用 3.泛型上的反射 4.自定义特性 5.特性构造器 6.具名参数 7.预定义特性 8.动态编程 特性(attribute)是在一个程序集中插入 ...
- 安卓开发之viewpager学习(头条显示)
activity_luancher.xml代码如下: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res ...