hdu 1507 Uncle Tom's Inherited Land*(二分图最大匹配)

本文探讨了由老家族传承的土地分割成网格后的销售策略,通过应用二分图匹配算法来最大化可售地块数量,同时确保遵守当地法规,避免销售包含水塘的地块。文章详细介绍了输入数据格式、解决方案步骤及输出格式,为土地资源有效管理和销售提供了实用方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Uncle Tom's Inherited Land*

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2185    Accepted Submission(s): 905
Special Judge


Problem Description
Your old uncle Tom inherited a piece of land from his great-great-uncle. Originally, the property had been in the shape of a rectangle. A long time ago, however, his great-great-uncle decided to divide the land into a grid of small squares. He turned some of the squares into ponds, for he loved to hunt ducks and wanted to attract them to his property. (You cannot be sure, for you have not been to the place, but he may have made so many ponds that the land may now consist of several disconnected islands.)

Your uncle Tom wants to sell the inherited land, but local rules now regulate property sales. Your uncle has been informed that, at his great-great-uncle's request, a law has been passed which establishes that property can only be sold in rectangular lots the size of two squares of your uncle's property. Furthermore, ponds are not salable property.

Your uncle asked your help to determine the largest number of properties he could sell (the remaining squares will become recreational parks).

 

Input
Input will include several test cases. The first line of a test case contains two integers N and M, representing, respectively, the number of rows and columns of the land (1 <= N, M <= 100). The second line will contain an integer K indicating the number of squares that have been turned into ponds ( (N x M) - K <= 50). Each of the next K lines contains two integers X and Y describing the position of a square which was turned into a pond (1 <= X <= N and 1 <= Y <= M). The end of input is indicated by N = M = 0.
 

Output
For each test case in the input your program should first output one line, containing an integer p representing the maximum number of properties which can be sold. The next p lines specify each pair of squares which can be sold simultaneity. If there are more than one solution, anyone is acceptable. there is a blank line after each test case. See sample below for clarification of the output format.
 

Sample Input
  
4 4 6 1 1 1 4 2 2 4 1 4 2 4 4 4 3 4 4 2 3 2
 

Sample Output
  
4 (1,2)--(1,3) (2,1)--(3,1) (2,3)--(3,3) (2,4)--(3,4) 3 (1,1)--(2,1) (1,2)--(1,3) (2,3)--(3,3)
 

Source
题目分析:
相邻的不是池塘的块连边,然后二分图匹配,最后的匹配数就是答案,利用二分匹配中的linker数组记录相匹配的点,然后输出匹配方案。
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#define MAX 107

using namespace std;

int n,m,k,x,y,cnt;
int tx[MAX],ty[MAX];
int mp[MAX][MAX];
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};

int used[MAX];
int linker[MAX];

struct Edge
{
    int v,next;
}e[MAX*MAX];

int head[MAX];
int cc;

void add ( int u , int v )
{
    e[cc].v = v;
    e[cc].next = head[u];
    head[u] = cc++;
}

bool dfs ( int u )
{
    for ( int i = head[u] ; ~i ; i = e[i].next )
    {
        int v = e[i].v;
        if ( used[v] ) continue;
        used[v] = 1;
        if ( linker[v] == -1 || dfs ( linker[v] ) )
        {
            linker[v] = u;
            return true;
        }
    }
    return false;
}

int hungary ( )
{
    int res = 0;
    memset ( linker , -1 ,sizeof ( linker ) );
    for ( int i = 1 ; i < cnt ; i++ )
    {
        memset ( used , 0 , sizeof ( used ) );
        if ( dfs ( i ) ) res++;
    }
    return res/2;
}

int main ( )
{
    while ( ~scanf ( "%d%d" , &n , &m ) )
    {
        if ( !n && !m ) break;
        memset ( head , -1 , sizeof ( head ) );
        cc = 0;
        memset ( mp , 0 , sizeof ( mp ) );
        scanf ( "%d" , &k );
        for ( int i = 1 ; i <= n ; i++ )
            for ( int j = 1 ; j <= m ; j++ )
                mp[i][j] = 1; 
        for ( int i = 0 ; i < k ; i++ )
        {
            scanf ( "%d%d" , &x , &y );
            mp[x][y] = 0;
        }
        cnt = 1;
        for ( int i = 1 ; i <= n ; i++ )
            for ( int j = 1 ; j <= m ; j++ )
                if ( mp[i][j] )
                {
                    tx[cnt] = i;
                    ty[cnt] = j;
                    mp[i][j] = cnt++;
                }
        for ( int i = 1 ; i <= n ; i++ )
            for ( int j = 1 ; j <= m ; j++ )
                if ( mp[i][j] ) 
                    for ( int k = 0 ; k < 4 ; k++ )
                    {
                        int x = i + dx[k];
                        int y = j + dy[k];
                        int id1 = mp[i][j];
                        int id2 = mp[x][y];
                        if ( mp[x][y] )
                            add ( id1 , id2 );
                    }
        printf ( "%d\n" , hungary ( ) );
        memset ( used , 0 , sizeof ( used ) );
        for ( int i = 1 ; i < cnt ; i++ )
        {
            int id = linker[i];
            if ( id == -1 ) continue;
            if ( used[i] | used[id] ) continue;
            used[i] = used[id] = 1;
            printf ( "(%d,%d)--(%d,%d)\n" , tx[i] , ty[i] , tx[id] , ty[id] );
        } 
        puts ("");
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值