POJ 2289 Jamie's Contact Groups(二分+最大流)
2016-02-08 15:07
330 查看
题意:有n个人要把他们分成m个组,给出每个人可以分到的组的编号,每个人只能分到一个组,怎样分配能使最大组人最少,输出最大组的人的数量
思路:首先题目要求最大组最少,这种一般都是二分。
建图:0为源点,每个人到源点连一条容量为1的边,每个人和能分配的组连一条容量为1的边(注意组的编号是从0开始的,要处理一下),二分最大组人数t,每个组和汇点n+m+1连一条容量为t的边,最后跑一遍最大流看看是否等于n即可
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
Sample Output
思路:首先题目要求最大组最少,这种一般都是二分。
建图:0为源点,每个人到源点连一条容量为1的边,每个人和能分配的组连一条容量为1的边(注意组的编号是从0开始的,要处理一下),二分最大组人数t,每个组和汇点n+m+1连一条容量为t的边,最后跑一遍最大流看看是否等于n即可
#include <cstdio> #include <queue> #include <cstring> #include <iostream> #include <cstdlib> #include <algorithm> #include <vector> #include <map> #include <string> #include <set> #include <ctime> #include <cmath> #include <cctype> using namespace std; #define maxn 1550 #define INF 1<<29 #define LL long long int cas=1,T; struct Edge { int from,to,cap,flow; Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){} }; int n,m; struct Dinic { // int n,m; int s,t; vector<Edge>edges; //边数的两倍 vector<int> G[maxn]; //邻接表,G[i][j]表示结点i的第j条边在e数组中的序号 bool vis[maxn]; //BFS使用 int d[maxn]; //从起点到i的距离 int cur[maxn]; //当前弧下标 void init() { for (int i=0;i<=n+m+1;i++) G[i].clear(); edges.clear(); } void AddEdge(int from,int to,int cap) { edges.push_back(Edge(from,to,cap,0)); edges.push_back(Edge(to,from,0,0)); //反向弧 int mm=edges.size(); G[from].push_back(mm-2); G[to].push_back(mm-1); } bool BFS() { memset(vis,0,sizeof(vis)); queue<int>q; q.push(s); d[s]=0; vis[s]=1; while (!q.empty()) { int x = q.front();q.pop(); for (int i = 0;i<G[x].size();i++) { Edge &e = edges[G[x][i]]; if (!vis[e.to] && e.cap > e.flow) { vis[e.to]=1; d[e.to] = d[x]+1; q.push(e.to); } } } return vis[t]; } int DFS(int x,int a) { if (x==t || a==0) return a; int flow = 0,f; for(int &i=cur[x];i<G[x].size();i++) { Edge &e = edges[G[x][i]]; if (d[x]+1 == d[e.to] && (f=DFS(e.to,min(a,e.cap-e.flow)))>0) { e.flow+=f; edges[G[x][i]^1].flow-=f; flow+=f; a-=f; if (a==0) break; } } return flow; } int Maxflow(int s,int t) { this->s=s; this->t=t; int flow = 0; while (BFS()) { memset(cur,0,sizeof(cur)); flow+=DFS(s,INF); } return flow; } }dc; struct List { char name[20]; int gro[501]; }list[maxn]; bool solve(int t) { dc.init(); for (int i = 1;i<=n;i++) //每个人到源点连边 { dc.AddEdge(0,i,1); for (int j=1;j<=list[i].gro[0];j++) dc.AddEdge(i,list[i].gro[j]+1+n,1); } for (int i = 1;i<=m;i++) dc.AddEdge(n+i,n+m+1,t); return dc.Maxflow(0,n+m+1) == n; } int main() { //freopen("in","r",stdin); while (scanf("%d%d",&n,&m)!=EOF && n) { dc.init(); for (int i = 1;i<=n;i++) { // dc.AddEdge(0,i,1); int j = 1; scanf("%s",list[i].name); while (1) { scanf("%d",&list[i].gro[j]); // dc.AddEdge(i,list[i].gro[j]+1+n,1); j++; char ch=getchar(); if (ch == '\n') { list[i].gro[0]=j-1; break; } } } int l = 0; int r = n; while (l<r) { int mid = l+(r-l)/2; if (solve(mid)) r=mid; else l=mid+1; } printf("%d\n",r); } //printf("time=%.3lf",(double)clock()/CLOCKS_PER_SEC); return 0; }
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
相关文章推荐
- 读卡器项目总结(三)— I2C通信
- Kinect For Windows V2开发日志六:人体的轮廓的表示
- python的拷贝(深拷贝和浅拷贝)
- apt-get 安装路径
- 百元买百鸡
- Python成长之路第二篇(2)_列表元组内置函数用法
- 第八章 JVM性能监控与故障处理工具(2)
- 开灯问题
- R语言学习(一)
- -boundingRectWithSize:options:attributes:context:用法
- ThinkPHP - I 函数
- 分数化小数
- 3、随机森林
- Python 第五篇(下):系统标准模块(shutil、logging、shelve、configparser、subprocess、xml、yaml、自定义模块)
- Redis缓存服务器Java客户端Jedis2.8 demo
- c 编程中的exit(-1)和 return -1
- 1.2.1Logistic回归实例
- ThinkPHP - 模板使用函数
- win10的酒店上网记
- 2016小目标