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

来源:互联网 发布:天气预报软件哪个最好? 编辑:程序博客网 时间:2024/06/06 02:18

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

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


#include<cstdio>#include<cstring>#include<vector>using namespace std;const int maxn=500+5;#define INF 1e9struct Max_Match{    int n,m;    vector<int> g[maxn];    bool vis[maxn];    int left[maxn];    void init(int n)    {        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;int cas=1,T;int n,m;struct Node{int w,l,h;}node[maxn];bool check(int i,int j){return (node[i].w<node[j].w && node[i].l<node[j].l && node[i].h<node[j].h);}int main(){while (scanf("%d",&n)!=EOF && n){MM.init(n);for (int i = 1;i<=n;i++)scanf("%d%d%d",&node[i].w,&node[i].l,&node[i].h);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;}

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

35 4 827 10 10100 32 52331 2 12 1 11 1 241 1 12 3 23 2 24 4 40
 

Sample Output

132
 


0 0
原创粉丝点击