(二分+匈牙利算法) hdu 2236

无题II

Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1198    Accepted Submission(s): 535

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

Author

xhd

Source

HDOJ 2008 Summer Exercise(2)- Hold by Captain Xu

直接二分差值,然后直接匈牙利算法判断是否符合即可

#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<string>
#define INF 100000000
using namespace std;
int n,mp[105][105],minn,maxx,link[105],mark[105],l,r,mid,p;
bool dfs(int x)
{
    for(int i=1;i<=n;i++)
    {
        if(mark[i]==-1&&mp[x][i]>=p&&mp[x][i]<=p+mid)
        {
            mark[i]=1;
            if(link[i]==-1||dfs(link[i]))
            {
                link[i]=x;
                return true;
            }
        }
    }
    return false;
}
bool check(int x)
{
    memset(link,-1,sizeof(link));
    for(int i=1;i<=n;i++)
    {
        memset(mark,-1,sizeof(mark));
        if(!dfs(i))
            return false;
    }
    return true;
}
int main()
{
    int tt;
    scanf("%d",&tt);
    while(tt--)
    {
        scanf("%d",&n);
        maxx=0,minn=INF;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                scanf("%d",&mp[i][j]);
                minn=min(minn,mp[i][j]);
                maxx=max(maxx,mp[i][j]);
            }
        }
        l,r,mid;
        int ans=0;
        l=0,r=maxx-minn;
        while(l<=r)
        {
            mid=(l+r)>>1;
            bool flag=false;
            for(p=minn;p+mid<=maxx;p++)
            {
                if(check(mid))
                {
                    flag=true;
                    break;
                }
            }
            if(flag)
            {
                ans=mid;
                r=mid-1;
            }
            else
                l=mid+1;
        }
        printf("%d\n",ans);
    }
    return 0;
}

  

时间: 2024-08-08 16:37:20

(二分+匈牙利算法) hdu 2236的相关文章

(匈牙利算法) hdu 1281

棋盘游戏 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2757    Accepted Submission(s): 1612 Problem Description 小希和Gardon在玩一个游戏:对一个N*M的棋盘,在格子里放尽量多的一些国际象棋里面的“车”,并且使得他们不能互相攻击,这当然很简单,但是Gardon限制了只有某些格

(匈牙利算法) hdu 5093

Battle ships Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 553    Accepted Submission(s): 223 Problem Description Dear contestant, now you are an excellent navy commander, who is responsible o

(tarjan+匈牙利算法) hdu 3861

F - The King’s Problem Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 3861 Description In the Kingdom of Silence, the king has a new problem. There are N cities in the kingdom and there are M d

(匈牙利算法) hdu 2119

#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<cmath> #include<algorithm> #include<cstdlib> using namespace std; int n,m,a[110][110],g[110][110],mark[110],link[110],ans; bool dfs(i

(匈牙利算法) hdu 4185

Oil Skimming Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 993    Accepted Submission(s): 422 Problem Description Thanks to a certain "green" resources company, there is a new profitable

(匈牙利算法) hdu 2063

过山车 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 12125    Accepted Submission(s): 5302 Problem Description RPG girls今天和大家一起去游乐场玩,终于可以坐上梦寐以求的过山车了.可是,过山车的每一排只有两个座位,而且还有条不成文的规矩,就是每个女生必须找个个男生做par

HDU 2063:过山车(二分匹配,匈牙利算法)

过山车 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 9745    Accepted Submission(s): 4294 Problem Description RPG girls今天和大家一起去游乐场玩,终于可以坐上梦寐以求的过山车了.可是,过山车的每一排只有两个座位,而且还有条不成文的规矩,就是每个女生必须找个个男生做par

HDU 1150:Machine Schedule(二分匹配,匈牙利算法)

Machine Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5371    Accepted Submission(s): 2658 Problem Description As we all know, machine scheduling is a very classical problem in compu

【01染色法判断二分匹配+匈牙利算法求最大匹配】HDU The Accomodation of Students

http://acm.hdu.edu.cn/showproblem.php?pid=2444 [DFS染色] 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<string> 5 #include<cmath> 6 #include<algorithm> 7 8 using namespace std; 9 const int maxn=2e2