博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 2289——Jamie's Contact Groups——————【多重匹配、二分枚举匹配次数】
阅读量:5241 次
发布时间:2019-06-14

本文共 3959 字,大约阅读时间需要 13 分钟。

 

 

Jamie's Contact Groups
Time Limit:7000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit     

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 2John 0 1Rose 1Mary 15 4ACM 1 2 3ICPC 0 1Asian 0 2 3Regional 1 2ShangHai 0 20 0

Sample Output

22 题目大意:给你n个人可能的分组,给你m个组。问你将n个人分到组中,且每个人只能分到一个组内,最大组(组内人数最多)的最小值是多少。 解题思路:多重匹配。这是一对多的情况。很明显是多重匹配,但是匹配次数却没有,而是让求的结果。那我们就枚举匹配次数。如果该匹配次数满足条件,记录答案,同时向小逼近,如果不满足,就向大逼近。
#include
#include
#include
#include
#include
using namespace std;const int maxn = 1010;int Map[maxn][maxn];//linker[v][j]表示第j个与Y部的v匹配的是x部的谁int linker[maxn][maxn], used[maxn];int mid;bool dfs(int u,int rn){ for(int v = 1; v <= rn; v++){ if(used[v] || !Map[u][v]){ continue; } used[v] = 1; if(linker[v][0] < mid){ //枚举的匹配次数 linker[v][++linker[v][0]] = u; return true; }else{ for(int j = 1; j <= linker[v][0]; j++){ if(dfs(linker[v][j],rn)){ linker[v][j] = u; return true; } } } } return false;}bool Hungary(int ln,int rn){ int ret = 0; for(int i = 0; i <= rn; i++){ linker[i][0] = 0; } for(int i = 1; i <= ln; i++){ memset(used,0,sizeof(used)); if(dfs(i,rn)){ ret++; } } if(ln == ret){ return true; } return false;}int main(){ int n,m; char str[5000]; while(scanf("%d%d",&n,&m)!=EOF&&(n+m)){ memset(Map,0,sizeof(Map)); getchar(); for(int i = 1; i <= n; i++){ gets(str); int len = strlen(str); int v = 0, flag = 1; for(int j = 0; j <= len; j++){ if(str[j] >= '0'&& str[j] <= '9'){ v = v*10 + str[j]-'0'; flag = 0; }else{ if(flag) continue; else{ Map[i][v+1] = 1; v = 0; } } } } int l = 1, r = n; while(l < r){ mid = (l+r)/2; if(Hungary(n,m)){ r = mid; }else{ l = mid + 1; } } printf("%d\n",r); } return 0;}

  

 

转载于:https://www.cnblogs.com/chengsheng/p/4961210.html

你可能感兴趣的文章
CentOS7安装iptables防火墙
查看>>
mysql-5.7 innodb 的并行任务调度详解
查看>>
shell脚本
查看>>
Upload Image to .NET Core 2.1 API
查看>>
python针对excel的读写操作-----openpyxl
查看>>
最后几本书,不珍藏了。
查看>>
Js时间处理
查看>>
Java项目xml相关配置
查看>>
按钮实现A标签新窗口打开(不用window.open)
查看>>
Array对象
查看>>
MainActivity
查看>>
三维变换概述
查看>>
android 数字键盘制作
查看>>
Cpp: object lifetime
查看>>
[8.08考试] 隔膜
查看>>
Board Game CodeForces - 605D (BFS)
查看>>
Python 中 open()文件操作的方式
查看>>
实验三
查看>>
Android开发高手课 - 02 崩溃优化(下):应用崩溃了,你应该如何去分析?
查看>>
jenkins:忘记登录密码怎么办
查看>>