(beginer) 网络流(最大流+二分) UVA 1345 - Jamie\'s Contact Groups

来源:互联网 发布:鬼影 电影 知乎 编辑:程序博客网 时间:2024/05/16 09:28

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 2John 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


题意:主人公有很名字,想要对其进行分组,但是每个名字只能分配到他能分配到的第一个组中,组数已经确定为M个,问怎么分配能使得最多人那个组的人数最少。

思路:我们用二分法确定组的最大人数,然后用一次最大流,看最大流是不是n,如果是就将限制人数进一步减少。如果不行,就放宽。建图方法是:加一个超级源点,连向所有的人,容量为1,人和对应的组也连一条边,容量也为1,组连向一个超级汇点,容量需要你二分枚举来确定。

代码:
#include<iostream>#include<cstdio>#include<vector>#include<queue>#include<string.h>#include<cstring>using namespace std;const int maxn = 3000+5;const int inf = 1e9;int n , m;struct Edge{int u , v;int flow , cap;Edge(int uu,int vv,int fl,int ca) : u(uu) , v(vv) , flow(fl) , cap(ca) { }};vector<Edge> edge;struct ISAP{int s , t;int d[maxn];int cur[maxn];int p[maxn];int num[maxn];vector<int> G[maxn];vector<Edge> edge;int flow;void init() {for (int i = 0 ; i <= n+m+1 ; ++i) G[i].clear();s = 0 , t = n+m+1;}bool bfs() {memset(d,-1,sizeof(d));queue<int> q;q.push(t);d[t] = 0;while (q.size()){int u = q.front(); q.pop();for (int i = 0 ; i < G[u].size() ; ++i) {Edge & e = edge[G[u][i]];if (e.cap == 0 && d[e.v]==-1) {d[e.v] = d[u] + 1;q.push(e.v);}}}return d[s]!=-1;}int Augment() {int x = t , a = inf;while (x!=s) {Edge & e = edge[p[x]];a = min(a,e.cap-e.flow);x = edge[p[x]].u;}x = t;while (x!=s) {edge[p[x]].flow += a;edge[p[x]^1].flow -= a;x = edge[p[x]].u;}return a;}int maxflow(int s,int t) {this->s = s , this->t = t;flow = 0;bfs();memset(num,0,sizeof(num));for (int i = 0 ; i <= n+m+1 ; ++i) num[d[i]]++;int x = s;memset(cur,0,sizeof(cur));while (d[s] < n+m+2) {if (x==t) {flow += Augment();x = s;}bool ok = false;for (int i = cur[x] ; i < G[x].size() ; ++i) {Edge & e = edge[G[x][i]];if (e.cap > e.flow && d[x]==d[e.v]+1) {ok = true;p[e.v] = G[x][i];cur[x] = i;x = e.v;break;}}if (!ok) {int k = n+m+1;for (int i = 0 ; i < G[x].size() ; ++i) {Edge & e = edge[G[x][i]];if (e.cap>e.flow) k = min(k,d[e.v]);}if (--num[d[x]]==0) break;num[d[x]=k+1]++;cur[x] = 0;if (x!=s) x = edge[p[x]].u;}}return flow;}}solver;char buffer[11000];void add(int s,int t,int cap){edge.push_back(Edge(s,t,0,cap));edge.push_back(Edge(t,s,0,0));int x = edge.size();solver.G[s].push_back(x-2);solver.G[t].push_back(x-1);}void input(){solver.init();int x;edge.clear();for (int i = 1 ; i <= n ; ++i) {add(0,i,1);scanf("%*s");gets(buffer);char * p = strtok(buffer, " ");while (p) {sscanf(p,"%d",&x);add(i,x+1+n,1);p = strtok(NULL," ");}}int size = edge.size();for (int i = n+1 ; i <= n+m ; ++i) {size += 2;solver.G[i].push_back(size-2);solver.G[n+m+1].push_back(size-1);}}void BuildGraph(int up){solver.edge = edge;for (int i = n+1 ; i <= n+m ; ++i)  {solver.edge.push_back(Edge(i,n+m+1,0,up));solver.edge.push_back(Edge(n+m+1,i,0,0));}}void solve(){int left = 1 , right = n;int mid = (left+right)>>1;int ans = n;while (left <= right) {BuildGraph(mid);if (solver.maxflow(0,n+m+1)==n) {right = mid-1;ans = min(ans,mid);} else {left = mid+1;}mid = (left+right)>>1;}printf("%d\n",ans);}int main(){while (scanf("%d%d",&n,&m)==2,n+m){input();solve();}}
0 0
原创粉丝点击