您的位置:首页 > 其它

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

2014-02-19 12:19 267 查看


Dolls

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 929    Accepted Submission(s): 443


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

 

Recommend

lcy

这个题目还是比较好做的,这是我做的第一个二分匹配,题意是说有n个洋娃娃,

每个娃娃有长宽高,长宽高都大的可以包含小的,求有多少个在外面的,用匈牙利

算法求出最大匹配值,然后用n减去就的了

#include<stdio.h>
#include<string.h>
using namespace std;
#define M 2000
int n;
int link[M],g[M][M];//g[][]是算地图,link代表联系
bool vis[M];
struct node
{
int x,y,z;
}box[M];
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 num=0;
for(int i=1;i<=n;i++)
{
memset(vis,false,sizeof(vis));
if(find(i))
{
//printf("fdsjklfsd");
num++;
}

}
printf("%d\n",n-num);
}
return 0;
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息