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

来源:互联网 发布:自学编程入门教材 编辑:程序博客网 时间:2024/05/22 10:46

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
35 4 827 10 10100 32 52331 2 12 1 11 1 241 1 12 3 23 2 24 4 40
 

Sample Output
132
刚见到这个题,想到了以前做过的一个题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来实现。具体怎么使用我就不多说了。
 

原创粉丝点击