poj 3189(枚举加最大流)
2011-09-30 09:29
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.
题意是这样的: 农夫约翰有n个牛,m个牛棚,每个牛都有对m个牛棚的一个排序,表示自己住在其中的舒适程度。。。你的任务就是找出一个牛棚居住的安排。。。让舒适度最小的和最大的差值最小。。。。
这个题刚开始做的时候。。。构不出来图。。。没去想暴力枚举。。。问了一下别人。。然后发现大部分都是枚举做的。。然后构图都是在枚举的时候满足枚举条件的连线就刚好了。。。好吧。。。这个题不是自己真正意义上做出来的。。。。囧
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 3611 | Accepted: 1245 |
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.
题意是这样的: 农夫约翰有n个牛,m个牛棚,每个牛都有对m个牛棚的一个排序,表示自己住在其中的舒适程度。。。你的任务就是找出一个牛棚居住的安排。。。让舒适度最小的和最大的差值最小。。。。
这个题刚开始做的时候。。。构不出来图。。。没去想暴力枚举。。。问了一下别人。。然后发现大部分都是枚举做的。。然后构图都是在枚举的时候满足枚举条件的连线就刚好了。。。好吧。。。这个题不是自己真正意义上做出来的。。。。囧
#include <iostream> #include<cstdio> #include<string> using namespace std; #define inf 1<<30 #define maxM 100005 #define maxN 1050 #define cc(m,v) memset(m,v,sizeof(m)) struct node{ int u,v,f,next; }edge[maxM]; int head[maxN],p,lev[maxN],cur[maxN]; int que[maxM],maze[1005][30],cap[30]; void ainit(){ p=0,cc(head,-1); } bool bfs(int s,int t){ int u,i,v,qin=0,qout=0; cc(lev,0),lev[s]=1,que[qin++]=s; while(qout!=qin){ u=que[qout++]; for(i=head[u];i!=-1;i=edge[i].next) if(edge[i].f>0 && lev[v=edge[i].v]==0){ lev[v]=lev[u]+1,que[qin++]=v; if(v==t) return 1; } } return lev[t]; } int dinic(int s,int t){ int i,k,u,f; int flow=0,qin; while(bfs(s,t)){ memcpy(cur,head,sizeof(head)); u=s,qin=0; while(1){ if(u==t){ for(k=0,f=inf;k<qin;k++) if(edge[que[k]].f<f) f=edge[que[i=k]].f; for(k=0;k<qin;k++) edge[que[k]].f-=f,edge[que[k]^1].f+=f; flow+=f,u=edge[que[qin=i]].u; } for(i=cur[u];cur[u]!=-1;i=cur[u]=edge[cur[u]].next) if(edge[i].f>0 && lev[u]+1==lev[edge[i].v]) break; if(cur[u]!=-1) que[qin++]=cur[u],u=edge[cur[u]].v; else{ if(qin==0) break; lev[u]=-1,u=edge[que[--qin]].u; } } } return flow; } void addedge(int u,int v,int f){ edge[p].u=u,edge[p].v=v,edge[p].f=f,edge[p].next=head[u],head[u]=p++; edge[p].u=v,edge[p].v=u,edge[p].f=0,edge[p].next=head[v],head[v]=p++; } void cal(int n,int m){ int i,j,u,v; for(i=1;i<=m;i++) for(j=1;j+i-1<=m;j++){ ainit(); for(u=1;u<=n;u++) addedge(0,u,1); for(u=1;u<=m;u++) addedge(u+n,n+m+1,cap[u]); for(u=1;u<=n;u++) for(v=j;v<j+i;v++) addedge(u,maze[u][v]+n,1); if(dinic(0,n+m+1)==n){ printf("%d\n",i); return ; } } } int main(){ int n,m,i,j; scanf("%d%d",&n,&m); for(i=1;i<=n;i++) for(j=1;j<=m;j++) scanf("%d",&maze[i][j]); for(i=1;i<=m;i++) scanf("%d",&cap[i]); cal(n,m); return 0; }
相关文章推荐
- POJ 3189 枚举+最大流
- poj 3189 二分+枚举+最大流
- poj 3189 Steady Cow Assignment(最大流,枚举)
- POJ 3189--Steady Cow Assignment【二分图多重匹配 && 最大流求解 && 枚举 && 经典】
- POJ 3189 枚举+最大流判可行性
- poj 3189 Steady Cow Assignment 【最大流】【枚举上下界 判断是否满流】
- POJ 3189 最大流 枚举
- 【最大流+dinic+二分枚举】北大 poj 3189 Steady Cow Assignment
- POJ - 3189 Steady Cow Assignment 二分 + 枚举 + (最大流||二分图多重匹配)
- poj 3189(枚举+最大流)
- poj 3189 Steady Cow Assignment(多重匹配or二分最大流)
- O - Steady Cow Assignment - POJ 3189(多重匹配+枚举)
- POJ3189 Steady Cow Assignment 二分枚举+dinic最大流
- poj 3189(枚举+多重匹配)
- POJ 3189——Steady Cow Assignment——————【多重匹配、二分枚举区间长度】
- POJ-2112 Floyd+二分枚举+最大流(or 匈牙利)
- poj 2289 二分枚举+最大流
- POJ 2391 Ombrophobic Bovines 不喜欢雨的奶牛 Floyd+二分枚举+最大流
- POJ 3498 March of the Penguins(枚举+最大流)
- poj 3498(枚举加最大流dinic)