HDU 4160 二分图最小点覆盖

       比赛的时候做道题一直用Dp做,一直wr,,,一直wr了十几次最后还是没有过。。。。。。。。。。。。。最后才知道原来是二分图最小点集覆盖,完全想错了。。。。。。。。。。。崩溃!

        建图很容易,就是如果一个玩具能放到另一个玩具里面,则这两个玩具之间建边。之后求二分图最小点集覆盖就可以了。

        题目让求最后最少剩下多少个玩具,即为最少用多少个玩具把这些玩具全部覆盖。题目:

Dolls

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 106    Accepted Submission(s): 49


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
 
ac代码:

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <string.h>
#include <vector>
using namespace std;
struct matrix{
  int len,wid,height;
}aa[510];
int flag[510],visted[510];
vector<int> map[510];
bool dfs(int x){
  //visted[x]=1;
  for(int i=0;i<map[x].size();++i){
    int j=map[x][i];
    if(!visted[j]){
      visted[j]=1;
      if(flag[j]==-1||dfs(flag[j])){
        flag[j]=x;
        return true;
      }
    }
  }
  return false;
}
int main(){
  //freopen("1.txt","r",stdin);
  int n;
  while(scanf("%d",&n)&&n){
    memset(map,0,sizeof(map));
    memset(flag,-1,sizeof(flag));
    for(int i=1;i<=n;++i)
        scanf("%d%d%d",&aa[i].wid,&aa[i].len,&aa[i].height);
    for(int i=1;i<=n;++i){
        for(int j=1;j<=n;++j){
            if(aa[i].wid<aa[j].wid&&aa[i].len<aa[j].len&&aa[i].height<aa[j].height)
                map[i].push_back(j);
        }
    }
    int count=0;
    for(int i=1;i<=n;++i){
      memset(visted,0,sizeof(visted));
      if(dfs(i))
          count++;
    }
    printf("%d\n",n-count);
  }
  return 0;
}


你可能感兴趣的:(HDU 4160 二分图最小点覆盖)