您的位置:首页 > 其它

HDU 4160 Dolls(DAG最小路径覆盖)

2017-08-23 15:41 465 查看
HDU 4160 Dolls(DAG最小路径覆盖)
http://acm.hdu.edu.cn/showproblem.php?pid=4160
题意:

       有N个木偶,木偶有3项指标,w,i,h. 如果第i个木偶的3项指标对应小于第j个木偶的3项指标,那么i木偶可以放到j木偶中. 且一个木偶里面只能直接的放一个别的木偶.问你这N个木偶最优嵌套的方案下,最多有几个木偶不能被任何木偶嵌套?

分析:

       如果i木偶能放在j木偶中,那么连一条i->j的有向边. 那么最终我们能得到一个DAG图. 现在我们的问题是要在该DAG图中找最少的简单路径,这些路径没有交集且正好完全覆盖了DAG的各个顶点.(想想是不是这个问题)

       上面这个问题就是DAG的最小路径覆盖问题,我们可以通过建立二分图来求解. DAG的最小路径覆盖 = N-二分图最大匹配数.

AC代码:

#include<cstdio>
#include<cstring>
#include<vector>
#include<iostream>
using namespace std;
const int maxn=600+5;
#define inf 0x3f3f3f3f
struct Max_Match
{
int n,m;
vector<int>g[maxn];
bool vis[maxn];
int left[maxn];

void init(int n,int m)
{
this->n=n;
this->m=m;
for(int i=1;i<=n;i++)
g[i].clear();
memset(left,-1,sizeof(left));
}

bool match(int u)
{
for(int i=0;i<g[u].size();i++)
{
int v=g[u][i];
if(!vis[v])
{
vis[v]=true;
if(left[v]==-1 || match(left[v]))
{
left[v]=u;
return true;
}
}
}

return false;
}

int solve()
{
int ans=0;
for(int i=1;i<=n;i++)
{
memset(vis,0,sizeof(vis));
if(match(i)) ans++;
}
return ans;
}
}MM;

struct node
{
int l,h,w;
}s[maxn];
bool check(int i,int j)
{
if(s[i].h>s[j].h&&s[i].l>s[j].l&&s[i].w>s[j].w)
return true;

return false;
}
int main()
{
int n,w,l,h;
while(~scanf("%d",&n)&&n)
{
for(int i=1;i<=n;i++)
scanf("%d%d%d",&s[i].w,&s[i].l,&s[i].h);
MM.init(n,n);
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
if(i!=j&&check(i,j))
{
MM.g[i].push_back(j);

}
}
printf("%d\n",n-MM.solve());
}
return 0;
}
/*
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
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: