HDU 4160 Dolls 【最小路径覆盖】

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
分析:二分图最大匹配,匈牙利算法,题意是 给出N个套娃,一个套娃只有在长宽高都小于另一个套娃的情况下才能放入另一个套娃,可以将其转化为二分图最小路径覆盖
一个有向无环图的最小路径覆盖=n-最大匹配数
建图的时候,如果一个套娃可以放在另一个里面就在其间建立一条边
code:
View Code
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define clr(x)memset(x,0,sizeof(x))
#define maxn 505
struct node
{
int x,y,z;
}q[maxn];
int cmp(const void*p1,const void*p2)
{
struct node*c=(struct node*)p1;
struct node*d=(struct node*)p2;
if(c->x==d->x)
{
if(c->y==d->y)
return c->z-d->z;
else return c->y-d->y;
}
else return c->x-d->x;
}
bool v[maxn];
bool g[maxn][maxn];
int link[maxn];
int n;
int find(int k)
{
int i;
for(i=1;i<=n;i++)
{
if(g[k][i]&&!v[i])
{
v[i]=1;
if(link[i]==0||find(link[i]))
{
link[i]=k;
return 1;
}
}
}
return 0;
}
int main()
{
int i,j,tot;
while(scanf("%d",&n),n)
{
clr(link); clr(g);
for(i=1;i<=n;i++)
scanf("%d%d%d",&q[i].x,&q[i].y,&q[i].z);
qsort(q+1,n,sizeof(struct node),cmp);
for(i=1;i<n;i++)
{
for(j=i+1;j<=n;j++)
if(q[i].x<q[j].x&&q[i].y<q[j].y&&q[i].z<q[j].z)
g[i][j]=1;
}
tot=0;
for(i=1;i<=n;i++)
{
clr(v);
if(find(i))
tot++;
}
printf("%d\n",n-tot);
}
return 0;
}

posted @ 2012-03-15 01:00  'wind  阅读(233)  评论(0编辑  收藏  举报