Dolls
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1572 Accepted Submission(s): 768
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
Source
The 2011 Syrian Collegiate Programming Contest
题目大意:
给你N个套娃,一只套娃可以包容下另一个套娃的条件是:当前套娃的长宽高都比那个套娃大,并且每只套娃都不能旋转。问最少暴露在外边的套娃有多少个。
思路:
1、如果一只套娃可以容纳另一只套娃,那么我们将两者之间建立一条边,表示这只套娃可以容纳另外一只套娃。
假设有这样一种情况(其中箭头指向表示这个套娃可以容纳另外一只套娃):
明显其最小路径覆盖有两条边:
表示上边三个套娃可以用第一个暴露在外边,下边两只套娃也用第一个暴露在外边,明显最小路径数==答案。
2、那么最小路径数==总套娃数-最大匹配数,那么我们只要建图跑二分匹配匈牙利算法即可。
Ac代码:
#include<stdio.h>
#include<string.h>
#include<vector>
using namespace std;
struct node
{
int x,y,z;
}a[5050];
vector<int >mp[5050];
int vis[5050];
int match[5050];
int find(int u)
{
for(int i=0;i<mp[u].size();i++)
{
int v=mp[u][i];
if(vis[v]==0)
{
vis[v]=1;
if(match[v]==-1||find(match[v]))
{
match[v]=u;
return 1;
}
}
}
return 0;
}
int main()
{
int n;
while(~scanf("%d",&n))
{
if(n==0)break;
memset(match,-1,sizeof(match));
for(int i=0;i<n;i++)mp[i].clear();
for(int i=0;i<n;i++)
{
scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].z);
}
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
if(a[i].x>a[j].x&&a[i].y>a[j].y&&a[i].z>a[j].z)
{
mp[i].push_back(j);
}
}
}
int output=0;
for(int i=0;i<n;i++)
{
memset(vis,0,sizeof(vis));
if(find(i)==1)output++;
}
printf("%d\n",n-output);
}
}