poj 3189(枚举+最大流)
2011-09-02 15:07
423 查看
Steady Cow Assignment
Description
Farmer John's N (1 <= N <= 1000) cows each reside in one of B (1 <= B <= 20) barns which, of course, have limited capacity. Some cows really like their current barn, and some are not so happy.
FJ would like to rearrange the cows such that the cows are as equally happy as possible, even if that means all the cows hate their assigned barn.
Each cow gives FJ the order in which she prefers the barns. A cow's happiness with a particular assignment is her ranking of her barn. Your job is to find an assignment of cows to barns such that no barn's capacity is exceeded and the size of the range (i.e.,
one more than the positive difference between the the highest-ranked barn chosen and that lowest-ranked barn chosen) of barn rankings the cows give their assigned barns is as small as possible.
Input
Line 1: Two space-separated integers, N and B
Lines 2..N+1: Each line contains B space-separated integers which are exactly 1..B sorted into some order. The first integer on line i+1 is the number of the cow i's top-choice barn, the second integer on that line is the number of the i'th cow's second-choice
barn, and so on.
Line N+2: B space-separated integers, respectively the capacity of the first barn, then the capacity of the second, and so on. The sum of these numbers is guaranteed to be at least N.
Output
Line 1: One integer, the size of the minumum range of barn rankings the cows give their assigned barns, including the endpoints.
Sample Input
Sample Output
Hint
Explanation of the sample:
Each cow can be assigned to her first or second choice: barn 1 gets cows 1 and 5, barn 2 gets cow 2, barn 3 gets cow 4, and barn 4 gets cows 3 and 6.
Source
USACO 2006 February Gold
题目:http://poj.org/problem?id=3189
分析:这题要求最小的分数区间,满足所有牛能住下,我们可以枚举区间长度和区间分数,把在该区间内的牛和牛棚连上边,牛和源连上边,牛棚和汇连上边,最大流判断是否满足要求。。。
代码:
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 3533 | Accepted: 1214 |
Farmer John's N (1 <= N <= 1000) cows each reside in one of B (1 <= B <= 20) barns which, of course, have limited capacity. Some cows really like their current barn, and some are not so happy.
FJ would like to rearrange the cows such that the cows are as equally happy as possible, even if that means all the cows hate their assigned barn.
Each cow gives FJ the order in which she prefers the barns. A cow's happiness with a particular assignment is her ranking of her barn. Your job is to find an assignment of cows to barns such that no barn's capacity is exceeded and the size of the range (i.e.,
one more than the positive difference between the the highest-ranked barn chosen and that lowest-ranked barn chosen) of barn rankings the cows give their assigned barns is as small as possible.
Input
Line 1: Two space-separated integers, N and B
Lines 2..N+1: Each line contains B space-separated integers which are exactly 1..B sorted into some order. The first integer on line i+1 is the number of the cow i's top-choice barn, the second integer on that line is the number of the i'th cow's second-choice
barn, and so on.
Line N+2: B space-separated integers, respectively the capacity of the first barn, then the capacity of the second, and so on. The sum of these numbers is guaranteed to be at least N.
Output
Line 1: One integer, the size of the minumum range of barn rankings the cows give their assigned barns, including the endpoints.
Sample Input
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
Sample Output
2
Hint
Explanation of the sample:
Each cow can be assigned to her first or second choice: barn 1 gets cows 1 and 5, barn 2 gets cow 2, barn 3 gets cow 4, and barn 4 gets cows 3 and 6.
Source
USACO 2006 February Gold
题目:http://poj.org/problem?id=3189
分析:这题要求最小的分数区间,满足所有牛能住下,我们可以枚举区间长度和区间分数,把在该区间内的牛和牛棚连上边,牛和源连上边,牛棚和汇连上边,最大流判断是否满足要求。。。
代码:
#include<cstdio> using namespace std; const int mm=222222; const int mn=2222; const int oo=1000000000; int node,src,dest,edge; int ver[mm],flow[mm],next[mm]; int head[mn],work[mn],dis[mn],q[mn]; int level[mn][22],cap[22],n,m; inline int min(int a,int b) { return a<b?a:b; } inline void prepare(int _node,int _src,int _dest) { node=_node,src=_src,dest=_dest; for(int i=0;i<node;++i)head[i]=-1; edge=0; } inline void addedge(int u,int v,int c) { ver[edge]=v,flow[edge]=c,next[edge]=head[u],head[u]=edge++; ver[edge]=u,flow[edge]=0,next[edge]=head[v],head[v]=edge++; } bool Dinic_bfs() { int i,u,v,l,r=0; for(i=0;i<node;++i)dis[i]=-1; dis[q[r++]=src]=0; for(l=0;l<r;++l) for(i=head[u=q[l]];i>=0;i=next[i]) if(flow[i]&&dis[v=ver[i]]<0) { dis[q[r++]=v]=dis[u]+1; if(v==dest)return 1; } return 0; } int Dinic_dfs(int u,int exp) { if(u==dest)return exp; for(int &i=work[u],v,tmp;i>=0;i=next[i]) if(flow[i]&&dis[v=ver[i]]==dis[u]+1&&(tmp=Dinic_dfs(v,min(exp,flow[i])))>0) { flow[i]-=tmp; flow[i^1]+=tmp; return tmp; } return 0; } int Dinic_flow() { int i,ret=0,delta; while(Dinic_bfs()) { for(i=0;i<node;++i)work[i]=head[i]; while(delta=Dinic_dfs(src,oo))ret+=delta; } return ret; } int solve() { int ans,i,j,k; for(ans=1;ans<=m;++ans) { for(k=1;k+ans-1<=m;++k) { prepare(n+m+2,0,n+m+1); for(i=1;i<=n;++i)addedge(src,i,1); for(i=1;i<=m;++i)addedge(i+n,dest,cap[i]); for(i=1;i<=n;++i) for(j=k;j<k+ans;++j)addedge(i,level[i][j]+n,1); if(Dinic_flow()==n)return ans; } } return m; } int main() { int i,j; while(scanf("%d%d",&n,&m)!=-1) { for(i=1;i<=n;++i) for(j=1;j<=m;++j)scanf("%d",&level[i][j]); for(i=1;i<=m;++i)scanf("%d",&cap[i]); printf("%d\n",solve()); } return 0; }
相关文章推荐
- 【最大流+dinic+二分枚举】北大 poj 3189 Steady Cow Assignment
- POJ 3189 枚举+最大流
- POJ - 3189 Steady Cow Assignment 二分 + 枚举 + (最大流||二分图多重匹配)
- poj 3189 二分+枚举+最大流
- poj 3189 Steady Cow Assignment(最大流,枚举)
- POJ 3189--Steady Cow Assignment【二分图多重匹配 && 最大流求解 && 枚举 && 经典】
- poj 3189(枚举加最大流)
- POJ 3189 枚举+最大流判可行性
- poj 3189 Steady Cow Assignment 【最大流】【枚举上下界 判断是否满流】
- POJ 3189 最大流 枚举
- poj 2455 Secret Milking Machine(二分枚举+最大流)
- 【POJ】1815 Friendship 枚举+最大流。。
- POJ3498最大流,枚举终点,企鹅,基础最大流
- POJ 3189 Steady Cow Assignment(最大流)
- POJ 1207 求最大数链长度 暴力枚举数学题
- POJ 3498 March of the Penguins(枚举+最大流)
- POJ 1815 最大流最小割枚举找割点
- SAP 算法 求最大流 poj 3189 pku
- POJ 2455--Secret Milking Machine【二分枚举 && 最大流 && 经典】
- POJ 1207 求最大数链长度 暴力枚举数学题