hdu 1669 Jamie's Contact Groups(二分+二分图最大多重匹配)
2016-07-28 10:18
274 查看
Jamie's Contact Groups
Time Limit: 15000/7000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 551 Accepted Submission(s): 189
Problem 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
Source
2004 Asia Regional Shanghai
题意:有n个人和m个地方,每个人可以属于多个地方,问怎么样选择每个人去哪个地方才可以得到人数最多的地方人最少
思路:要求人数最多的地方人最少,明显的要用二分法,枚举人数最多的人数即可。然后每次都多重匹配,满足就往下移动,不满足就往上
代码:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std; #define N 1100 #define M 550 int ma [M]; int n,m; int line[M] ,vis[M],num[M],cnt[M]; int can(int t) { for(int i=0; i<m; i++) { if(!vis[i]&&ma[t][i]) { vis[i]=1; if(cnt[i]<num[i]) { line[i][cnt[i]++]=t; return 1; } else { for(int j=0; j<num[i]; j++) { if(can(line[i][j])) { line[i][j]=t; return 1; } } } } } return 0; } int main() { char op[20],s[100000]; while(~scanf("%d %d",&n,&m)&&(n+m)) { memset(ma,0,sizeof(ma)); for(int i=1; i<=n; i++) { scanf("%s ",op); gets(s); int id=0,len=strlen(s); for(int j=0; j<len; j++) { if(s[j]>='0'&&s[j]<='9') { id=id*10+s[j]-'0'; if(j==len-1) ma[i][id]=1; } else if(j>0&&s[j-1]>='0'&&s[j-1]<='9') { ma[i][id]=1; id=0; } } } int l=0,r=2000,last; while(l<=r) { int mid=(l+r)>>1; for(int i=0; i<m; i++) num[i]=mid; memset(cnt,0,sizeof(cnt)); int ans=0; for(int i=1; i<=n; i++) { memset(vis,0,sizeof(vis)); if(can(i)) ans++; else break; } if(ans<n) l=mid+1; else last=mid,r=mid-1; } printf("%d\n",last); } return 0; }
相关文章推荐
- Android Notification
- js/jq模拟点击事件
- NYOJ_1036 非洲小孩 HDU_1687 Lucky Light
- appCloud 基本用法
- 降低Ie浏览器版本
- 进程与线程的一个简单解释
- linux下源码安装R-3.1.2.tar.gz和RStudio开发工具
- 字符流中第一个不重复的字符
- java泛型编译时被擦除引起多态的破坏,用 桥方法解决此类问题。(java 桥方法)
- Duilib创建窗口双击标题栏禁止窗口最大化
- [Objective-C]关联(objc_setAssociatedObject、objc_getAssociatedObject、objc_removeAssociatedObjects)
- Pro JPA2读书笔记系列(九)-第九章(条件API)
- 数据结构实验之链表九:双向链表
- Thrift架构简介
- 中文乱码问题,utf-8与ISO-8859-1之间的转换
- DJi GuidanceSDK调研报告
- VC中如何配置可调试项目
- 题目14 会场安排问题
- 静态代理与动态代理
- Linux iostat监测IO状态