zoukankan      html  css  js  c++  java
  • POJ1274 The Perfect Stall 二分图,匈牙利算法

            N头牛,M个畜栏,每头牛仅仅喜欢当中的某几个畜栏,可是一个畜栏仅仅能有一仅仅牛拥有,问最多能够有多少仅仅牛拥有畜栏。

            典型的指派型问题,用二分图匹配来做,求最大二分图匹配能够用最大流算法,也能够用匈牙利算法,这里使用匈牙利算法。

    #include <stdlib.h>
    #include <stdio.h>
    #include <vector>
    #include <math.h>
    #include <string.h>
    #include <string>
    #include <iostream>
    #include <queue>
    #include <list>
    #include <algorithm>
    #include <stack>
    #include <map>
    
    #include<iostream>  
    #include<cstdio>  
    using namespace std;
    
    #define	MAXN	401
    
    bool Visited[MAXN];
    int Result[MAXN];
    int G[MAXN][MAXN];
    
    bool dfs(int s, int n, int m)
    {
    
    	for (int i = n + 1; i <= n + m;i++)
    	{
    		if (G[s][i] && Visited[i] == false)
    		{
    			Visited[i] = true;
    			if (Result[i] == 0 ||dfs(Result[i], n, m))
    			{
    				Result[i] = s;
    				return true;
    			}
    		}
    	}
    	return false;
    }
    
    int main()
    {
    #ifdef _DEBUG
    	freopen("d:\in.txt", "r", stdin);
    #endif
    	int n, m;
    	while (scanf("%d %d", &n, &m) != EOF)
    	{
    		memset(G, 0, sizeof(G));
    		memset(Result, 0, sizeof(Result));
    		for (int i = 1; i <= n;i++)
    		{
    			int k;
    			scanf("%d", &k);
    			for (int j = 0; j < k; j++)
    			{
    				int d;
    				scanf("%d", &d);
    				G[i][n + d] = 1;
    			}
    			
    		}
    		int max = 0;
    		for (int s = 1; s <= n; s++)
    		{
    			memset(Visited, 0, sizeof(Visited));
    			if (dfs(s, n, m))
    			{
    				max++;
    			}
    			
    		}
    		printf("%d
    ", max);
    	}
    	return 0;
    }


  • 相关阅读:
    c3p0 空指针异常 com.mchange.v2.resourcepool.CannotAcquireResourceException: A ResourcePool could not acquire a resource from its primary factory or source.
    HTML02单词
    HTML01
    java JVM虚拟机
    线程范围内的数据共享
    电话面试
    IDEA快捷键
    Intellij IDEA 生成返回值对象快捷键
    IDEA 快捷将创建main函数
    模块
  • 原文地址:https://www.cnblogs.com/wzjhoutai/p/7086310.html
Copyright © 2011-2022 走看看