uva 1345 Jamie's Contact Groups (最大流+二分)
2015-08-08 11:21
471 查看
uva 1345 Jamie’s Contact Groups
DescriptionJamie 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
题目大意:给出一些人,和他们可能加入的组号。问每个组,最小的最大人数是多少。
解题思路:看到最小的最大值就想到二分,问题是二分什么。这题二分的是最大流的容量。设置一个超级源点,连向所有的人,容量为1。设置一个超级汇点,使所有的组连向超级汇点,二分的就是这里的容量(0~n)。然后根据题目给出的人和组的关系连接人和组,容量为1。二分时,若当前状态求出的最大流等于人数,则下界等于mid,若不等于,则上界等于mid。二分出的容量,就是组中的最小的最大人数。
[code]#include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <cstdlib> #include <sstream> #include <iostream> using namespace std; const int N = 1010 + 510; const int M = 1010 * 510 * 4; const int PO = 1005; const int INF = 0x3f3f3f3f; typedef long long ll; int n, m, s, t, rec[PO][PO], cnt[PO]; int ec, head , first , que , lev ; int Next[M], to[M], v[M]; void init() { ec = 0; memset(first, -1, sizeof(first)); } void addEdge(int a,int b,int c) { to[ec] = b; v[ec] = c; Next[ec] = first[a]; first[a] = ec++; to[ec] = a; v[ec] = 0; Next[ec] = first[b]; first[b] = ec++; } int BFS() { int kid, now, f = 0, r = 1, i; memset(lev, 0, sizeof(lev)); que[0] = s, lev[s] = 1; while (f < r) { now = que[f++]; for (i = first[now]; i != -1; i = Next[i]) { kid = to[i]; if (!lev[kid] && v[i]) { lev[kid] = lev[now] + 1; if (kid == t) return 1; que[r++] = kid; } } } return 0; } int DFS(int now, int sum) { int kid, flow, rt = 0; if (now == t) return sum; for (int i = head[now]; i != -1 && rt < sum; i = Next[i]) { head[now] = i; kid = to[i]; if (lev[kid] == lev[now] + 1 && v[i]) { flow = DFS(kid, min(sum - rt, v[i])); if (flow) { v[i] -= flow; v[i^1] += flow; rt += flow; } else lev[kid] = -1; } } return rt; } int dinic() { int ans = 0; while (BFS()) { for (int i = 0; i <= t; i++) { head[i] = first[i]; } ans += DFS(s, INF); if (ans >= n) return ans; } return ans; } void input() { init(); s = 0, t = (n + m) + 2; memset(cnt, 0 ,sizeof(cnt)); char ch[100]; int num; string ss; for (int i = 0; i < n; i++) { getline(cin, ss); stringstream stream(ss); stream >> ch; while (stream >> num) { addEdge(i + 1, num + 1 + n, 1); } } for (int i = 1; i <= n; i++) { addEdge(s, i, 1); } for (int i = 1; i <= m; i++) { addEdge(i + n, t, 1); } } int build(int x) { for (int i = 0; i < ec; i += 2) { if (to[i] == t) v[i] = x; else v[i] = 1; v[i^1] = 0; } int temp = dinic(); if (temp == n) return 1; else return 0; } int main() { while (scanf("%d %d\n", &n, &m) == 2, (n || m)) { input(); int L = 0, R = n + 2; while (L < R) { int mid = (L + R) / 2; if (build(mid)) R = mid; else L = mid + 1; } printf("%d\n", L); } return 0; }
相关文章推荐
- swift新手入门视频教程-08-枚举
- 一次tomcat应用无法访问的处理过程
- iOS8 tableView的Cell高度自适应开发
- 理解面向消息的中间件和JMS
- 关于字符串和数组字符串
- 【Neural Network】林轩田机器学习技法
- 【DFS】POJ3009-Curling 2.0
- HDU 5353 Average 贪心
- 微信红包要收税 到底关我卵事?
- 南邮 OJ 2098 3_A+B(III)
- HDU 1879 继续畅通工程
- 转 wsdl
- UIWebView网页视图—IOS开发
- volatile 关键字
- HDU - 3466 Proud Merchants(不理解)
- kali无法输入中文
- Cent OS下发送邮件
- eclipse如何查看类之间的引用关系
- onfiguration problem: Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/security]
- 如何生成vfh特征值pcd文件