HDU 4160 (ZZULI 1598) Dolls(二分…

来源:互联网 发布:yii2.0框架源码下载 编辑:程序博客网 时间:2024/06/05 08:53

Dolls

Time Limit:1000MS Memory Limit:65536K
Total Submit:48 Accepted:16

Description

Do you remember the box ofMatryoshka dolls last week? Adam just got another box of dolls fromMatryona. This time, the dolls have different shapes and sizes:some are skinny, some are fat, and some look as though they wereattened. Specifically, doll i can be represented by three numberswi, li, and hi, denoting its width, length, and height. Doll i canfit 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 insideanother. Of course, each doll may contain at most one doll rightinside it. Your goal is to fit dolls inside each other so that youminimize the number of outermost dolls.

Input

The input consists ofmultiple test cases. Each test case begins with a line with asingle integer N, 1 ≤ N ≤ 500, denoting the number of Matryoshkadolls. Then follow N lines, each with three space-separatedintegers wi, li, and hi (1 ≤ wi; li; hi ≤ 10,000) denoting the sizeof the ith doll. Input is followed by a single line with N = 0,which should not be processed.

Output

For each test case, printout a single line with an integer denoting the minimum number ofoutermost dolls that can be obtained by optimally nesting the givendolls.

SampleInput

35 4 827 10 10100 32 52331 2 12 1 11 1 241 1 12 3 23 2 24 4 40

SampleOutput

132

Hint

此题正确解法为二分图匹配

Source

 

这题本来以为是贪心呢,结果比赛的时候无论怎么做都提交不上,郁闷死了,快,后来听学长说是二分匹配最后看了二分匹配的算法才AC了

 

代码:

C语言:高亮代码由发芽网提供
C语言:高亮代码由发芽网提供

#include<stdio.h>
#include<string.h>

intflag[501],match[501][501],vis[501],n;
structnode{
intx,y,z;
}map[501];

intOK(int i,int j)
{
if(map[j].x<map[i].x&&map[j].y<map[i].y&&map[j].z<map[i].z)
return1;
return0;
}
intDFS(int k)
{
inti,j;
for(i=0;i<match[k][0];i++)
{
j=match[k][i];
if(!vis[j])
{
vis[j]=1;
if(!flag[j]||DFS(flag[j]))
{
flag[j]=k;
return1;
}
}
}
return0;
}
intmain()
{
inti,j,num;
while(scanf("%d",&n),n)
{
for(i=0;i<n;i++)
{
scanf("%d%d%d",&map[i].x,&map[i].y,&map[i].z);
match[i][0]=0;
}
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(OK(i,j))
match[i][match[i][0]++]=j;
num=0;
memset(flag,0,sizeof(flag));
for(i=0;i<n;i++)
{
memset(vis,0,sizeof(vis));
if(DFS(i))
num++;
}
printf("%d\n",n-num);
}
return0;
}
原创粉丝点击