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

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
*/



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值