(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
#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();}}
- (beginer) 网络流(最大流+二分) UVA 1345 - Jamie\'s Contact Groups
- UVA 1345 - Jamie's Contact Groups(二分+最大流)
- uva 1345 Jamie's Contact Groups (最大流+二分)
- UValive3268 Jamie's Contact Groups(二分+最大流)
- poj2289 Jamie's Contact Groups(二分答案+最大流)
- Jamie's Contact Groups(二分图多重匹配+二分)(网络流)
- POJ2289 Jamie's Contact Groups 二分+最大流匹配
- POJ 2289 - Jamie's Contact Groups 二分+最大流
- POJ 2289 Jamie's Contact Groups(二分+最大流)
- UVALive - 3268 Jamie's Contact Groups(二分+最大流)
- poj 2289 Jamie's Contact Groups 【二分 + 最大流】
- poj 2289 Jamie's Contact Groups【二分 + 最大流】
- POJ 2289 Jamie's Contact Groups(二分+最大流)
- Poj 2289 Jamie's Contact Groups 【最大流Dinic+二分】
- POJ 2289 Jamie's Contact Groups (二分+匹配/网络流)
- poj 2289 Jamie's Contact Groups(二分答案+网络流判定)
- poj 2289 Jamie's Contact Groups 二分+网络流
- 【POJ 2289】 Jamie's Contact Groups 【网络流+二分】
- (advanced) UVA 最小生成树(限制度数) UVA 1537 Picnic Planning
- (beginer) 最小权匹配 UVA 1045 - The Great Wall Game
- (beginer) 二分图(最大独立集) UVA 12168 Cat vs. Dog
- (intermediate) 二分图(边着色) UVA 10615 Rooks
- (beginer) 最小费用最大流 UVA 10806 - Dijkstra, Dijkstra.
- (beginer) 网络流(最大流+二分) UVA 1345 - Jamie\'s Contact Groups
- 《将博客搬至CSDN》
- (beginer) 网络流(拆点+时序模型) UVA 1161 - Objective: Berlin
- (beginer) 网络流(拆点)UVA 12125 - March of the Penguins
- (beginer)网络流(最小割最大流) UVA 1212 - Duopoly
- (beginer) 网络流(区间模型+最大费用流) UVA 1317 Concert Hall Scheduling
- (beginer) 网络流(最小费用流+费用跟流量有关) UVA 1486 - Transportation
- (beginer) 网络流 UVA 10249 - The Grand Dinner
- OOC源码(二)