您的位置:首页 > 其它

POJ2289--Jamie's Contact Groups

2013-07-31 20:51 447 查看
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
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

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
using namespace std;
#define maxn 2080
#define edge 1100080
#define inf 0x3f3f3f3f
int first[maxn],dis[maxn],num[maxn];
int vv[edge],ww[edge],nxt[edge];
int e,NN,n,m;
int zu[1080][520],zunum[1080];
inline int min(int a,int b)
{
return a>b?b:a;
}
void addEdge(int u,int v,int w)
{
vv[e] = v;	ww[e] = w;		nxt[e] = first[u];	first[u] = e++;
vv[e] = u;	ww[e] = 0;		nxt[e] = first[v];		first[v] = e++;
}
int dfs(int u,int s,int d,int cost)
{
if(u == d)	return cost;
int _min = NN;
int ans = 0;
for(int i = first[u];i != -1;i = nxt[i])
{
int v = vv[i];
if(ww[i])
{
if(dis[v] + 1 == dis[u])
{
int t = dfs(v,s,d,min(ww[i],cost));
ww[i] -= t;
ww[i^1] += t;
ans += t;
cost -= t;
if(dis[s] == NN)		return ans;
if(!cost)	break;
}
if(_min > dis[v])		_min = dis[v];
}
}
if(!ans)
{
if(--num[dis[u]] == 0)	dis[s] = NN;
dis[u] = _min +1;
++num[dis[u]];
}
return ans;
}
int isap(int s,int d)
{
memset(dis,0,sizeof(dis));
memset(num,0,sizeof(num));
num[0] = NN;
int ans = 0;
while(dis[s] < NN)
ans += dfs(s,s,d,inf);
return ans;
}
void build(int mid)
{
memset(first,-1,sizeof(first));
e = 0;NN = 2*m + n + 2;
for(int i = 1;i <= n;i++)
{
addEdge(0,i,1);
}
for(int i = 1;i <= n;i++)
{
int j = 1;
for(int j = 1;j <= zunum[i];j++)
{
addEdge(i,n+zu[i][j],1);
}
}
for(int i = n+1;i <= n+m;i++)
{
addEdge(i,i+m,mid);
}
for(int i = n+1+m;i <= n+2*m;i++)
{
addEdge(i,n+2*m+1,inf);
}
}
int main()
{
//freopen("in.txt","r",stdin);
while(scanf("%d%d",&n,&m)==2 && (n||m))
{
for(int i = 1;i <= n;i++)
{
string a;
cin>>a;
int j = 1;
while(getchar()!='\n')
{
scanf("%d",&zu[i][j]);
zu[i][j]++;	j++;
}
zunum[i] = j-1;
}
int l = 0,r = n;
while(l < r)
{
int mid = (l+r) >> 1;
build(mid);
if(isap(0,n+2*m+1) >= n)	r = mid;
else l = mid+1;
}
printf("%d\n",l);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  POJ2289