zoukankan      html  css  js  c++  java
  • 匈牙利算法模板

    二分图的最大匹配问题n

    二分图最小定点覆盖n hdu1150

    DAG最小路径覆盖 m(节点数)-n

    二分图最大独立子集 m-n

    //匈牙利算法模板
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <cstring>
    #include <algorithm>
    #include <string>
    #include <stack>
    #include <queue>
    
    const int inf = (1<<31)-1;
    const int MAXN = 1e3;
    using namespace std;
    
    int g[MAXN][MAXN];
    int pre[MAXN]; //B的A节点
    int vist[MAXN];
    int n,m;
    
    bool find(int t1){
        for(int i=1;i<=m;i++){
            if(g[t1][i]&&vist[i]==0){//存在关系且,没有被访问过
                vist[i] = 1; //标记已经访问过的节点
                if(pre[i]==0||find(pre[i])){ //i节点没有访问过,或者i节点的pre节点能腾出位置
                    pre[i] = t1;
                    return true;
                }
            }
        }
        return false;
    }
    
    int main()
    {
        //int n,m;
        int x;
        while(~scanf("%d%d",&n,&m)){
            scanf("%d",&x);
            int t1,t2;
            memset(g,0,sizeof(g));
            for(int i=0;i<x;i++){
                scanf("%d%d",&t1,&t2);
                g[t1][t2] = 1;
            }
            memset(pre,0,sizeof(pre));
    
            int ct = 0;
            
            for(int i=1;i<=n;i++){
                memset(vist,0,sizeof(vist));
                if(find(i))ct++;
            }
            cout<<ct<<endl;
        }
    
        return 0;
    }
    
    /*
    4 4
    7
    1 1
    1 2
    2 2
    2 3
    3 1
    3 2
    4 3
    
    */
    View Code
    在一个谎言的国度,沉默就是英雄
  • 相关阅读:
    Linux进程关系
    ambari 卸载脚本
    CentOS-7.2安装Ambari-2.6.1
    MYSQL57密码策略修改
    CentOS7 离线安装MySQL
    centos 安装mysql Package: akonadi-mysql-1.9.2-4.el7.x86_64 (@anaconda)
    mysql 数据备份
    spring-boot-starter-thymeleaf对没有结束符的HTML5标签解析出错
    ssh: scp命令
    python:os.path
  • 原文地址:https://www.cnblogs.com/EdsonLin/p/5400663.html
Copyright © 2011-2022 走看看