zoukankan      html  css  js  c++  java
  • Pets(匈牙利算法)

    Are you interested in pets? There is a very famous pets shop in the center of the ACM city. There are totally m pets in the shop, numbered from 1 to m. One day, there are n customers in the shop, which are numbered from 1 to n. In order to sell pets to as more customers as possible, each customer is just allowed to buy at most one pet. Now, your task is to help the manager to sell as more pets as possible. Every customer would not buy the pets he/she is not interested in it, and every customer would like to buy one pet that he/she is interested in if possible.

    Input

    There is a single integer T in the first line of the test data indicating that there are T(T≤100) test cases. In the first line of each test case, there are three numbers n, m(0≤n,m≤100) and e(0≤e≤n*m). Here, n and m represent the number of customers and the number of pets respectively.

    In the following e lines of each test case, there are two integers x(1≤x≤n), y(1≤y≤m) indicating that customer x is not interested in pet y, such that x would not buy y.

    Output

    For each test case, print a line containing the test case number (beginning with 1) and the maximum number of pets that can be sold out.

    Sample Input

    1
    2 2 2
    1 2
    2 1
    

    Sample Output

    Case 1: 2
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #include<queue>
    #include<stack>
    #include<vector>
    #include<cmath>
    
    const int maxn=1e5+5;
    typedef long long ll;
    using namespace std;
    
    int n,m,k;
    int link[105];  
    bool visit[105],map[105][105];
    bool dfs(int a)
    {
        for(int i=1;i<=m;i++) 
          if(map[a][i]==0 && !visit[i])
          {
            visit[i]=1;
            if(link[i]==0 || dfs(link[i]))
            {
                link[i]=a;
                return true;
            }
          }
        return false;
    }
    int main()
    {
        int a,b,ans;
        int T;
        cin>>T;
        int cnt=1;
        while(T--)
        {
       // memset(visit,0,sizeof(visit));
        memset(link,0,sizeof(link));
        memset(map,0,sizeof(map));
        cin>>n>>m>>k;
        ans=0;
        for(int i=1;i<=k;i++)
        {
            scanf("%d%d",&a,&b);
            map[a][b]=1;
        }
        for(int i=1;i<=n;i++) 
        {
            memset(visit,0,sizeof(visit));
            if(dfs(i))
              ans++;
        }
        printf("Case %d: %d
    ",cnt++,ans);
        }
    }
  • 相关阅读:
    ASP.net MVC 构建layui管理后台(构造基础仓储)<1>
    ASP.net MVC 构建layui管理后台(整体效果)
    搭建私有 Nuget 服务器教程(1)
    SSAS数据集Cube不存在或者尚未处理
    浅谈MDX处理空值NULL及格式化结果
    Zoey.Dapper--Dapper扩展之把SQL语句放到文件中
    Polly每次重试执行不同的操作
    Exceptionless应用--自定义插件
    Fiddler开启Https的时候出现unable to configure windows to trust Fiddler Root certificate问题
    ASP.NET Core 中的配置
  • 原文地址:https://www.cnblogs.com/Staceyacm/p/10837764.html
Copyright © 2011-2022 走看看