hdu 4160 Dolls 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4160
图论 (匈牙利算法)
题目大意:俄罗斯套娃的样子,可以暂且看作是箱子(有长宽高三维)。
题目分析:双重for判断每两个箱子是否一个可以套到另一个里面,并以此作为邻接矩阵的构建依据。
code:
#include<cstdio>
#include<cstring>
int match[550],n;
bool g[550][550],mark[550];
struct node
{
int x,y,z;
}dolls[500];
bool judge(node a,node b)
{
return a.x>b.x&&a.y>b.y&&a.z>b.z;
}
bool hungary(int x)
{
for(int i=0;i<n;i++)
{
if(g[x][i]&&!mark[i])
{
mark[i]=true;
if(match[i]==-1||hungary(match[i]))
{
match[i]=x;
return true;
}
}
}
return false;
}
int main()
{
int i,j,sum;
while(scanf("%d",&n)!=EOF&&n)
{
for(i=0;i<n;i++)
{
scanf("%d%d%d",&dolls[i].x,&dolls[i].y,&dolls[i].z);
}
sum=0;
memset(g,false,sizeof(g));
memset(match,-1,sizeof(match));
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
if(judge(dolls[i],dolls[j]))g[i][j]=true;
}
}
for(i=0;i<n;i++)
{
memset(mark,false,sizeof(mark));
if(hungary(i))sum++;
}
printf("%d\n",n-sum);
}
return 0;
}
46MS 532K 883B
PS:自己已经能写了,思想还是一窍不通……TAT
本文介绍了 HDU 4160 Dolls 的解题思路及实现过程,采用匈牙利算法解决俄罗斯套娃类型的配对问题。通过双重 for 循环构建邻接矩阵,实现最大匹配。
139





