zoukankan      html  css  js  c++  java
  • HDU 2063 过山车 二分图题解

    一个男女搭配的关系图,看能够凑成多少对,基本和最原始的一个二分图谜题一样了,就是 一个岛上能够凑成多少对夫妻的问题。

    所以是典型的二分图问题。

    使用匈牙利算法,写成两个函数,就很清晰了。

    本程序还带分配释放程序,当然oj一般不须要。可是好的程序一定要。

    #include <stdio.h>
    #include <stdlib.h>
    int K, M, N, a, b;
    int *linker;
    bool **gra, *used;
    
    void initGraph()
    {
    	gra = (bool **) malloc(M * sizeof(bool*));//注意是bool*不是bool
    	for (int i = 1; i < M; i++)
    		gra[i] = (bool *) calloc(N, sizeof(bool));
    }
    void freeGraph()
    {
    	for (int i = 1; i < M; i++)
    		free(gra[i]);
    	free(gra);
    }
    
    bool hunDFS(int u)
    {
    	for (int v = 1; v < N; v++)
    	{
    		if (gra[u][v] && !used[v])
    		{
    			used[v] = true;
    			if (!linker[v] || hunDFS(linker[v]))
    			{
    				linker[v] = u;
    				return true;
    			}
    		}
    	}
    	return false;
    }
    
    int hungary()
    {
    	int ans = 0;
    	linker = (int *) calloc(N, sizeof(int));
    	for (int i = 1; i < M; i++)
    	{
    		used = (bool *) calloc(N, sizeof(bool));
    		if (hunDFS(i)) ans++;
    		free(used);
    	}
    	free(linker);
    	return ans;
    }
    
    int main()
    {
    	while (scanf("%d %d %d", &K, &M, &N) != EOF && K)
    	{
    		M++, N++;//下标从1開始,0不使用
    		initGraph();
    		while (K--)
    		{
    			scanf("%d %d", &a, &b);
    			gra[a][b] = true;
    		}
    		printf("%d
    ", hungary());
    		freeGraph();
    	}
    	return 0;
    }




  • 相关阅读:
    系统改造/升级项目的注意点
    phpunit相关
    IE浏览器:定义文档兼容性
    pptx,docx,xlsx 文件下载问题
    常用linux命令
    oracle 子查询写法
    QQ上传大文件为什么这么快
    decimal tostring 格式
    2020HDU多校第三场 1005 Little W and Contest
    火车进栈(进出栈的模拟,dfs爆搜)
  • 原文地址:https://www.cnblogs.com/zfyouxi/p/4297643.html
Copyright © 2011-2022 走看看