zoukankan      html  css  js  c++  java
  • POJ2289 Jamie's Contact Groups —— 二分图多重匹配/最大流 + 二分

    题目链接:https://vjudge.net/problem/POJ-2289

    Jamie's Contact Groups
    Time Limit: 7000MS   Memory Limit: 65536K
    Total Submissions: 8147   Accepted: 2736

    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

    题解:

    题意:jamie的QQ有n个联系人,且设置了m个分组,规定了哪些朋友可以去哪些分组。为了能够快速地找到朋友,jamie希望人数最多的分组的人数最少(最大值最小),并且满足每个朋友仅存在于一个分组中。

    1.二分最大值,即每个分组的容量。

    2.利用二分图多重匹配,或者最大流,求出是否所有人都可以归到一个分组中。如果可以,则减小容量,否则增大容量。

    多重匹配:

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cstdlib>
     5 #include <string>
     6 #include <vector>
     7 #include <map>
     8 #include <set>
     9 #include <queue>
    10 #include <sstream>
    11 #include <algorithm>
    12 using namespace std;
    13 const int INF = 2e9;
    14 const int MOD = 1e9+7;
    15 const int MAXM = 5e2+10;
    16 const int MAXN = 1e3+10;
    17 
    18 int uN, vN;
    19 int num[MAXM], linker[MAXM][MAXN];
    20 bool g[MAXN][MAXM], used[MAXM];
    21 
    22 bool dfs(int u)
    23 {
    24     for(int v = 0; v<vN; v++)
    25     if(g[u][v] && !used[v])
    26     {
    27         used[v] = true;
    28         if(linker[v][0]<num[v])
    29         {
    30             linker[v][++linker[v][0]] = u;
    31             return true;
    32         }
    33         for(int i = 1; i<=num[v]; i++)
    34         if(dfs(linker[v][i]))
    35         {
    36             linker[v][i] = u;
    37             return true;
    38         }
    39     }
    40     return false;
    41 }
    42 
    43 bool hungary(int mid)
    44 {
    45     for(int i = 0; i<vN; i++)
    46     {
    47         num[i] = mid;
    48         linker[i][0] = 0;
    49     }
    50     for(int u = 0; u<uN; u++)
    51     {
    52         memset(used, false, sizeof(used));
    53         if(!dfs(u)) return false;
    54     }
    55     return true;
    56 }
    57 
    58 char tmp[100000];
    59 int main()
    60 {
    61     while(scanf("%d%d", &uN, &vN) && (uN||vN))
    62     {
    63         memset(g, false, sizeof(g));
    64         getchar();
    65         for(int i = 0; i<uN; i++)
    66         {
    67             gets(tmp);
    68             int j = 0, len = strlen(tmp);
    69             while(tmp[j]!=' ' && j<len) j++;
    70             j++;
    71             for(int v = 0; j<=len; j++)
    72             {
    73                 if(tmp[j]==' '||j==len)
    74                 {
    75                     g[i][v] = true;
    76                     v = 0;
    77                 }
    78                 else v = v*10+(tmp[j]-'0');
    79             }
    80         }
    81 
    82         int l = 1, r = uN;
    83         while(l<=r)
    84         {
    85             int mid = (l+r)>>1;
    86             if(hungary(mid))
    87                 r = mid - 1;
    88             else
    89                 l = mid + 1;
    90         }
    91         printf("%d
    ", l);
    92     }
    93 }
    View Code

    最大流:

      1 #include <iostream>
      2 #include <cstdio>
      3 #include <cstring>
      4 #include <cstdlib>
      5 #include <string>
      6 #include <vector>
      7 #include <map>
      8 #include <set>
      9 #include <queue>
     10 #include <sstream>
     11 #include <algorithm>
     12 using namespace std;
     13 const int INF = 2e9;
     14 const int MOD = 1e9+7;
     15 const int MAXM = 5e2+10;
     16 const int MAXN = 2e3+10;
     17 
     18 struct Edge
     19 {
     20     int to, next, cap, flow;
     21 }edge[MAXN*MAXN];
     22 int tot, head[MAXN];
     23 
     24 int uN, vN, maze[MAXN][MAXN];
     25 int gap[MAXN], dep[MAXN], pre[MAXN], cur[MAXN];
     26 
     27 void add(int u, int v, int w)
     28 {
     29     edge[tot].to = v; edge[tot].cap = w; edge[tot].flow = 0;
     30     edge[tot].next = head[u]; head[u] = tot++;
     31     edge[tot].to = u; edge[tot].cap = 0; edge[tot].flow = 0;
     32     edge[tot].next = head[v]; head[v] = tot++;
     33 }
     34 
     35 int sap(int start, int end, int nodenum)
     36 {
     37     memset(dep, 0, sizeof(dep));
     38     memset(gap, 0, sizeof(gap));
     39     memcpy(cur, head, sizeof(head));
     40     int u = pre[start] = start, maxflow = 0,aug = INF;
     41     gap[0] = nodenum;
     42     while(dep[start]<nodenum)
     43     {
     44         loop:
     45         for(int i = cur[u]; i!=-1; i = edge[i].next)
     46         {
     47             int v = edge[i].to;
     48             if(edge[i].cap-edge[i].flow && dep[u]==dep[v]+1)
     49             {
     50                 aug = min(aug, edge[i].cap-edge[i].flow);
     51                 pre[v] = u;
     52                 cur[u] = i;
     53                 u = v;
     54                 if(v==end)
     55                 {
     56                     maxflow += aug;
     57                     for(u = pre[u]; v!=start; v = u,u = pre[u])
     58                     {
     59                         edge[cur[u]].flow += aug;
     60                         edge[cur[u]^1].flow -= aug;
     61                     }
     62                     aug = INF;
     63                 }
     64                 goto loop;
     65             }
     66         }
     67         int mindis = nodenum;
     68         for(int i = head[u]; i!=-1; i = edge[i].next)
     69         {
     70             int v=edge[i].to;
     71             if(edge[i].cap-edge[i].flow && mindis>dep[v])
     72             {
     73                 cur[u] = i;
     74                 mindis = dep[v];
     75             }
     76         }
     77         if((--gap[dep[u]])==0)break;
     78         gap[dep[u]=mindis+1]++;
     79         u = pre[u];
     80     }
     81     return maxflow;
     82 }
     83 
     84 bool test(int mid)
     85 {
     86     tot = 0;
     87     memset(head, -1, sizeof(head));
     88     for(int i = 0; i<uN; i++)
     89     {
     90         add(uN+vN, i, 1);
     91         for(int j = 0; j<vN; j++)
     92             if(maze[i][j])
     93                 add(i, uN+j, 1);
     94     }
     95     for(int i = 0; i<vN; i++)
     96         add(uN+i, uN+vN+1, mid);
     97 
     98     int maxflow = sap(uN+vN, uN+vN+1, uN+vN+2);
     99     return maxflow == uN;
    100 }
    101 
    102 char tmp[100000];
    103 int main()
    104 {
    105     while(scanf("%d%d", &uN, &vN) && (uN||vN))
    106     {
    107         memset(maze, 0, sizeof(maze));
    108         getchar();
    109         for(int i = 0; i<uN; i++)
    110         {
    111             gets(tmp);
    112             int j = 0, len = strlen(tmp);
    113             while(tmp[j]!=' ' && j<len) j++;
    114             j++;
    115             for(int v = 0; j<=len; j++)
    116             {
    117                 if(tmp[j]==' '||j==len)
    118                 {
    119                     maze[i][v] = 1;
    120                     v = 0;
    121                 }
    122                 else v = v*10+(tmp[j]-'0');
    123             }
    124         }
    125 
    126         int l = 1, r = uN;
    127         while(l<=r)
    128         {
    129             int mid = (l+r)>>1;
    130             if(test(mid))
    131                 r = mid - 1;
    132             else
    133                 l = mid + 1;
    134         }
    135         printf("%d
    ", l);
    136     }
    137 }
    View Code
  • 相关阅读:
    php中怎么使用call_user_func动态调用方法
    MYSQL中TIMESTAMP类型的默认值理解
    你可能不知道console强大
    在线文档
    PHP中字符串转换为数值 可能会遇到的坑
    网站推荐
    谷歌搜索语法
    ShuipFCMS -- 简单强大内容管理系统
    iWebShop -- 代替ecshop 的开源电子商务网站系统
    SuperSlide -- “焦点图/幻灯片”“Tab标签切换”“图片滚动”“无缝滚动”特效集成
  • 原文地址:https://www.cnblogs.com/DOLFAMINGO/p/7821830.html
Copyright © 2011-2022 走看看