hdu 2236 匹配

 

#include <stdio.h>
#include <string.h>
#define maxn 110
int nx,ny;
int map[maxn][maxn],ans,sx[maxn],sy[maxn];
int cx[maxn],cy[maxn];
int down;
int l,r,mid;
int path(int u)
{
    sx[u]=1;
    int v;
    for(v=1;v<=ny;v++)
    {
        if(map[u][v]>=down&&map[u][v]-down<=mid&&!sy[v])
        {
            sy[v]=1;
            if(!cy[v]||path(cy[v]))
            {
                cx[u]=v;cy[v]=u;
                return 1;
            }
        }
    }
    return 0;
}
int solve()
{
    ans=0;
    int i;
    memset(cx,0,sizeof(cx));
    memset(cy,0,sizeof(cy));
    for(i=1;i<=nx;i++)
    {
        if(!cx[i])
        {    
            memset(sx,0,sizeof(sx));
            memset(sy,0,sizeof(sy));
            ans+=path(i);
        }
    }
    return 0;
}
int main()
{
    int i,j;
    int t;
    int max=0,min=200;
    int top;
    int n;
    scanf("%d",&t);
    while(t--)
    {
        max=0,min=200;
        scanf("%d",&n);
        nx=ny=n;
        for(i=1;i<=n;i++)
            for(j=1;j<=n;j++)
            {
                scanf("%d",&map[i][j]);
                if(map[i][j]>max) max=map[i][j];
                if(map[i][j]<min) min=map[i][j];
            }
        top=max-min;
        l=0;r=top;
        mid=(l+r)/2;
        while(l<r)
        {
            for(down=min;down<=max;down++)
            {
                solve();
                if(ans==n)
                    break;
            }
            if(ans==n)
                r=mid;
            else l=mid+1;
            mid=(l+r)/2;
        }
        printf("%d\n",mid);
    }
    return 0;
}


 

 

posted @ 2013-12-15 13:16  贝尔摩德  阅读(118)  评论(0编辑  收藏  举报