zoukankan      html  css  js  c++  java
  • hdu2236:无题II(枚举+匈牙利算法)

    http://acm.hdu.edu.cn/showproblem.php?pid=2236

    Problem Description

    这是一个简单的游戏,在一个n*n的矩阵中,找n个数使得这n个数都在不同的行和列里并且要求这n个数中的最大值和最小值的差值最小。

    Input

    输入一个整数T表示T组数据。
    对于每组数据第一行输入一个正整数n(1<=n<=100)表示矩阵的大小。
    接着输入n行,每行n个数x(0<=x<=100)。

    Output

    对于每组数据输出一个数表示最小差值。

    Sample Input

    1
    4
    1 1 1 1
    2 2 2 2
    3 3 3 3
    4 4 4 4

    Sample Output

    3

    解题思路:

    找出最大值和最小值的差值,枚举0到该差值之间的值,找到最小的符合二分匹配的值就是答案。

    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    using namespace std;
    #define N 120
    int e[N][N], book[N], march[N], n, x, l;
    int dfs(int u)
    {
    	int v;	
    	for(v=1; v<=n; v++)
    	{
    		if(e[u][v]>=x && e[u][v]<=x+l && book[v]==0)
    		{
    			book[v]=1;
    			if(march[v]==0 || dfs(march[v]))
    			{
    				march[v]=u;
    				return 1;
    			}
    		}
    	}
    	return 0;
    }
    int main()
    {
    	int t, maxi, mini, i, j, q, temp, ans;
    	scanf("%d", &t);
    	while(t--)
    	{
    		maxi=0;
    		mini=99999999;
    		scanf("%d", &n);
    		for(i=1; i<=n; i++)
    			for(j=1; j<=n; j++)
    			{
    				scanf("%d", &e[i][j]);
    				maxi=max(maxi, e[i][j]);
    				mini=min(mini, e[i][j]);
    			}
    		q=maxi-mini;
    		l=0;
    		temp=0;
    		while(1)
    		{
    			for(x=mini; x+l<=maxi; x++)
    			{
    				memset(march, 0, sizeof(march));
    				ans=0;
    				for(i=1; i<=n; i++)
    				{
    					memset(book, 0, sizeof(book));
    					if(dfs(i))
    						ans++;	
    				} 
    				if(ans==n)
    				{
    					temp=1;	
    					break;
    				}		
    			}
    			if(temp==1)
    				break;
    			l++;
    		}
    
    		printf("%d
    ", l);
    	}
    	return 0;
    }
    
  • 相关阅读:
    mysql 远程登陆不上
    hdu 5339 Untitled【搜索】
    SqlServer 书目
    passwordauthentication yes
    oracle 11g RAC ocfs2
    Oracle 11g RAC database on ASM, ACFS or OCFS2
    CentOS ips bonding
    Oracle 11g RAC features
    openStack 王者归来之 trivial matters
    openstack windows 2008 img
  • 原文地址:https://www.cnblogs.com/zyq1758043090/p/11852627.html
Copyright © 2011-2022 走看看