POJ 2289 Jamie's Contact Groups (二分+dinic)
2013-03-14 18:25
447 查看
Jamie's Contact Groups
Description
Jamie is a very popular girl and has quite a lot of friends, so she always keeps a very long contact list in her cell phone. The contact list has become so long that it often takes a long time for her to browse through the whole list to find a friend's number.
As Jamie's best friend and a programming genius, you suggest that she group the contact list and minimize the size of the largest group, so that it will be easier for her to search for a friend's number among the groups. Jamie takes your advice and gives you
her entire contact list containing her friends' names, the number of groups she wishes to have and what groups every friend could belong to. Your task is to write a program that takes the list and organizes it into groups such that each friend appears in only
one of those groups and the size of the largest group is minimized.
Input
There will be at most 20 test cases. Ease case starts with a line containing two integers N and M. where N is the length of the contact list and M is the number of groups. N lines then follow. Each line contains a friend's name and the groups the friend could
belong to. You can assume N is no more than 1000 and M is no more than 500. The names will contain alphabet letters only and will be no longer than 15 characters. No two friends have the same name. The group label is an integer between 0 and M - 1. After the
last test case, there is a single line `0 0' that terminates the input.
Output
For each test case, output a line containing a single integer, the size of the largest contact group.
Sample Input
Sample Output
Source
Shanghai 2004
思路:之前有做过相同的题,比之前的简单,原理一样的,二分答案。对于每次的二分mid,对group到汇点t连接一条容量为mid的边,对每个人从源点s到每个人连接一条容量为1的边,每个人到他可能去的group连接一条容量为1的边,判断最大流是否满足n个人,是怎缩小范围更新,否则增大范围继续。
Time Limit: 7000MS | Memory Limit: 65536K | |
Total Submissions: 5630 | Accepted: 1776 |
Jamie is a very popular girl and has quite a lot of friends, so she always keeps a very long contact list in her cell phone. The contact list has become so long that it often takes a long time for her to browse through the whole list to find a friend's number.
As Jamie's best friend and a programming genius, you suggest that she group the contact list and minimize the size of the largest group, so that it will be easier for her to search for a friend's number among the groups. Jamie takes your advice and gives you
her entire contact list containing her friends' names, the number of groups she wishes to have and what groups every friend could belong to. Your task is to write a program that takes the list and organizes it into groups such that each friend appears in only
one of those groups and the size of the largest group is minimized.
Input
There will be at most 20 test cases. Ease case starts with a line containing two integers N and M. where N is the length of the contact list and M is the number of groups. N lines then follow. Each line contains a friend's name and the groups the friend could
belong to. You can assume N is no more than 1000 and M is no more than 500. The names will contain alphabet letters only and will be no longer than 15 characters. No two friends have the same name. The group label is an integer between 0 and M - 1. After the
last test case, there is a single line `0 0' that terminates the input.
Output
For each test case, output a line containing a single integer, the size of the largest contact group.
Sample Input
3 2 John 0 1 Rose 1 Mary 1 5 4 ACM 1 2 3 ICPC 0 1 Asian 0 2 3 Regional 1 2 ShangHai 0 2 0 0
Sample Output
2 2
Source
Shanghai 2004
思路:之前有做过相同的题,比之前的简单,原理一样的,二分答案。对于每次的二分mid,对group到汇点t连接一条容量为mid的边,对每个人从源点s到每个人连接一条容量为1的边,每个人到他可能去的group连接一条容量为1的边,判断最大流是否满足n个人,是怎缩小范围更新,否则增大范围继续。
#include<cstdio> #include<iostream> #include<cstring> using namespace std; const int mm=1003005; const int mn=2005; const int oo=1000000000; int node,s,t,edge; int to[mm],flow[mm],next[mm]; int head[mn],work[mn],dis[mn],q[mn]; int data[1005][505],gcnt[1005]; inline int min(int a,int b) { return a<b?a:b; } inline void init(int nn,int ss,int tt) { node=nn,s=ss,t=tt,edge=0; for(int i=0; i<node; ++i) head[i]=-1; } inline void add(int u,int v,int c) { to[edge]=v,flow[edge]=c,next[edge]=head[u],head[u]=edge++; to[edge]=u,flow[edge]=0,next[edge]=head[v],head[v]=edge++; } bool bfs() { int i,u,v,l,r=0; for(i=0; i<node; ++i) dis[i]=-1; dis[q[r++]=s]=0; for(l=0; l<r; ++l) for(i=head[u=q[l]]; i>=0; i=next[i]) if(flow[i]&&dis[v=to[i]]<0) { dis[q[r++]=v]=dis[u]+1; if(v==t)return 1; } return 0; } int dfs(int u,int maxf) { if(u==t) return maxf; for(int &i=work[u],v,tmp; i>=0; i=next[i]) if(flow[i]&&dis[v=to[i]]==dis[u]+1&&(tmp=dfs(v,min(maxf,flow[i])))>0) { flow[i]-=tmp; flow[i^1]+=tmp; return tmp; } return 0; } int dinic() { int i,ret=0,delta; while(bfs()) { for(i=0; i<node; ++i) work[i]=head[i]; while(delta=dfs(s,oo))ret+=delta; } return ret; } int main() { int i,j,n,m,g,ans; char c; while(scanf("%d%d",&n,&m)!=-1&&(n&&m)) { getchar(); memset(gcnt,0,sizeof(gcnt)); g=0; bool f=0; for(i=1; i<=n; ++i) { while((c=getchar())) { if(c>='0'&&c<='9') { g=g*10+(int)(c-'0'); f=1; } else if(f) { data[i][gcnt[i]++]=g+1+n; g=0,f=0; } if(c=='\n') break; } } int l=0,r=n,mid; ans=n; while(r>=l) { mid=(l+r)>>1; init(2+n+m,0,n+m+1); for(i=1; i<=n; i++) { add(s,i,1); for(j=0; j<gcnt[i]; j++) add(i,data[i][j],1); } for(i=n+1; i<=n+m; i++) add(i,t,mid); if(dinic()==n) { ans=mid; r=mid-1; } else l=mid+1; } printf("%d\n",ans); } return 0; }
相关文章推荐
- 【二分图多重匹配 && Dinic】POJ - 2289 Jamie's Contact Groups
- POJ 2289 Jamie's Contact Groups (二分答案+匹配流)
- Poj 2289 Jamie's Contact Groups 【最大流Dinic+二分】
- poj2289--Jamie's Contact Groups(二分多重匹配)
- POJ 2289 Jamie’s Contact Groups-二分匹配&多重匹配
- poj2289 Jamie's Contact Groups(二分答案+最大流)
- Poj 2289 Jamie's Contact Groups【二分+多重匹配】
- POJ 2289 Jamie's Contact Groups (二分答案+二分图的多重匹配)
- POJ 2289——Jamie's Contact Groups——————【多重匹配、二分枚举匹配次数】
- Jamie's Contact Groups POJ - 2289
- POJ2289 Jamie's Contact Groups(二分图多重匹配)
- POJ2289--Jamie's Contact Groups
- POJ 2289 Jamie's Contact Groups (二分+匹配/网络流)
- POJ 2289 Jamie's Contact Groups / UVA 1345 Jamie's Contact Groups / ZOJ 2399 Jamie's Contact Groups / HDU 1699 Jamie's Contact Groups / SCU 1996 Jamie's Contact Groups (二分,二分图匹配)
- POJ2289-Jamie's Contact Groups(二分图多重匹配)
- POJ 2289 Jamie's Contact Groups 多重匹配+二分
- poj 2289 Jamie's Contact Groups 【二分 + 最大流】
- poj 2289 Jamie's Contact Groups
- hdu 1669 Jamie's Contact Groups(二分+二分图最大多重匹配)
- UValive3268 Jamie's Contact Groups(二分+最大流)