Dolls
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 811 Accepted Submission(s): 378
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.
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
题意:有n个dolls,每个dolls都有长宽高。如果一个dolls的长宽高大于另一个dolls的长宽高,则较小的dolls可以放进较大的dolls里面,并且每个dolls里面只能放一个。让你求最外层的dolls有几个?
用总的dolls减去最大匹配的dolls。
#include<stdio.h>
#include<string.h>
using namespace std;
int n;
int link[1007],g[1007][1007];
bool vis[1007];
struct node
{
int x,y,z;
}box[1007];
bool judge(int i, int j)
{
if(box[i].x<box[j].x&&box[i].y<box[j].y&&box[i].z<box[j].z)
return true;
return false;
}
bool find(int i)
{
for(int j=1;j<=n;j++)
if(g[i][j]&&!vis[j])
{
vis[j]=true;
if(link[j]==0||find(link[j]))
{
link[j]=i;
return true;
}
}
return false;
}
int main()
{
while(scanf("%d",&n),n)
{
memset(g,0,sizeof(g));
memset(link,0,sizeof(link));
for(int i=1;i<=n;i++)
scanf("%d%d%d",&box[i].x,&box[i].y,&box[i].z);
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(judge(i,j))
g[i][j]=1;
int count=0;
for(int i=1;i<=n;i++)
{
memset(vis,false,sizeof(vis));
if(find(i))
count++;
}
printf("%d\n",n-count);
}
return 0;
}