hdu 4160 Dolls 匈牙利算法求最大匹配

Dolls

                                                                              Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

   题目链接
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
刚见到这个题,想到了以前做过的一个题 Wooden Sticks ,所以第一想法就是用 贪心 做,写好以后样例也过了,但是提交了两次都是 wrong answer ,却找不到出错误原因。后来想了下,决定换用另一种方法,因为刚学过 二分图 ,就试着用二分图来做。二分图最主要的就是如何建图。建图时,如果两个dools可以嵌套,则说明这两个dool有关系,所以就给这两个dool的编号连上一条边。求外面最少的doll,就相当于转化成了求 最小点击覆盖 ,我们只需要求出最大匹配,用总的个数减去最大匹配,问题就解决了。
AC代码:
/*用匈牙利算法二分图求最大匹配*/
#include<stdio.h>
#include<string.h>
#include<vector>
#include<algorithm>
using namespace std;
vector<int> vec[505];
int flag[505],vis[505];
struct doll
{
    int li,wi,hi;
}a[505];//定义结构体数组
bool comp(doll a1,doll a2)
{
    if(a1.wi==a2.wi)
      return a1.li<a2.li; //长度从小到大排序
    else
      return a1.wi<a2.wi; //宽度从小到大排序
    if(a1.li==a2.li)
      return a1.hi<a2.hi; //高度从小到大排序
    else
      return a1.li<a2.li;
}
bool find(int a) // 寻找最大匹配
{
    int i;
    for(i=0;i<vec[a].size();i++)
    {
        if(!vis[vec[a][i]])
        {
           vis[vec[a][i]]=1;
           if(flag[vec[a][i]]==0||find(flag[vec[a][i]]))
           {
               flag[vec[a][i]]=a;
               return true;
           }
        }
    }
    return false;
}
int main()
{
    int n,i,j,count;
    while(scanf("%d",&n)!=EOF&&n!=0)
    {
        for(i=0;i<n;i++)
          scanf("%d%d%d",&a[i].wi,&a[i].li,&a[i].hi);
        sort(a,a+n,comp);
        memset(vec,0,sizeof(vec));
        memset(flag,0,sizeof(flag)); //数组清零
        count=0; //记录最大匹配
        for(i=0;i<n;i++)
         for(j=i+1;j<n;j++)
         {
             if(a[j].li>a[i].li&&a[j].wi>a[i].wi&&a[j].hi>a[i].hi) //若可以嵌套,说明有边连接
              vec[i].push_back(j); //建立关系
         }
        for(i=0;i<n;i++)
        {
            memset(vis,0,sizeof(vis)); //记得每次都要清零
            if(find(i))
            count++; //找到新的匹配,数量加1
        }
        printf("%d\n",n-count); //输出不相关即不能嵌套的doll数量
    }
    return 0;
}


用匈牙利算法求最大匹配时,一般使用邻接矩阵来求,即用一个map[i][j]的二维数组,但是如果有用的边较少的话,我们就浪费了很多时间去遍历那些无用的边,所以用邻接表来优化,邻接表用STL中的vector来实现。具体怎么使用我就不多说了。
 
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值