hdu 4160 Dolls 匈牙利模板题

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

Dolls

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 494    Accepted Submission(s): 221

Problem Description
Do you remember the box of Matryoshka dolls last week? Adam just got another box of dolls from Matryona. This time, the dolls have different shapes and sizes: some are skinny, some are fat, and some look as though they were attened. Specifically, doll i can be represented by three numbers wi, li, and hi, denoting its width, length, and height. Doll i can fit inside another doll j if and only if wi < wj , li < lj , and hi < hj . That is, the dolls cannot be rotated when fitting one inside another. Of course, each doll may contain at most one doll right inside it. Your goal is to fit dolls inside each other so that you minimize the number of outermost dolls.
 
Input
The input consists of multiple test cases. Each test case begins with a line with a single integer N, 1 ≤ N ≤ 500, denoting the number of Matryoshka dolls. Then follow N lines, each with three space-separated integers wi, li, and hi (1 ≤ wi; li; hi ≤ 10,000) denoting the size of the ith doll. Input is followed by a single line with N = 0, which should not be processed.
 
Output
For each test case, print out a single line with an integer denoting the minimum number of outermost dolls that can be obtained by optimally nesting the given dolls.
 
Sample Input
3 5 4 8 27 10 10 100 32 523 3 1 2 1 2 1 1 1 1 2 4 1 1 1 2 3 2 3 2 2 4 4 4 0
 
Sample Output
1 3 2
 
Source
 
Recommend
lcy
 
用匈牙利算法找出各个盒子的包含情况(最大匹配数), 再用盒子总数减去最大匹配数就得出了最小覆盖数。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define max 510

    struct  d
    {
        int w,l,h;
    }doll[max];

int match[max][max];
int link[max];
bool used[max];
 int n;
 
bool  dfs(int u)
{
    for(int v=1;v<=n;v++)
        if(match[u][v]==1&&!used[v])
        {
            used[v]=true;
            if(link[v]==-1||dfs(link[v]))
            {
                link[v]=u;
                return true;
            }
        }
    return false;
}

int Xiong()
{
    int res=0;
    memset(link,-1,sizeof(link));
    for(int i=1;i<=n;i++)
    {
        memset(used,false,sizeof(used));
            if(dfs(i))
                res++;
    }
    return res;
}

int main()
{
    int i,j;
    while(scanf("%d",&n)==1&&n)
    {
        for(i=1;i<=n;i++)
            scanf("%d%d%d",&doll[i].w,&doll[i].l,&doll[i].h);
        
        memset(match,0,sizeof(match));                      //关键的建图,若i能包含j,则建立图中的match[i][j]=1
         for(i=1;i<=n;i++) 
             for(j=1;j<=n;j++)
                 if(doll[i].w>doll[j].w&&doll[i].l>doll[j].l&&doll[i].h>doll[j].h)
                     match[i][j]=1;

         printf("%d\n",n-Xiong());        
    }

    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值