Dolls
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1040 Accepted Submission(s): 496
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
题目意思:
有n个箱子,每个箱子长宽高分别为l,w,h。若l[i]<l[j]&&w[i]<w[j]&&h[i]<h[j],那么箱子 i 可以放到箱子 j 里面,经过一些操作使一些箱子放进另外一些箱子中,求最终漏在外面的箱子个数最小多少。
思路:
把包含的箱子弄到一个集合,把被包含的箱子弄到另外一个集合,二分最大匹配是最佳方案,设最大二分匹配为num,由于每有一个匹配,漏在外面的箱子数-1,那么最终答案为n-num。
代码:
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <vector> 5 #include <iostream> 6 using namespace std; 7 #define N 550 8 9 vector<int>ve[N]; 10 int from[N]; 11 int visited[N]; 12 int n; 13 14 struct node{ 15 int w, l, h; 16 }a[N]; 17 18 int mark(int u){ 19 int i, v; 20 for(i=0;i<ve[u].size();i++){ 21 v=ve[u][i]; 22 if(!visited[v]){ 23 visited[v]=1; 24 if(from[v]==-1||mark(from[v])){ 25 from[v]=u; 26 return 1; 27 } 28 } 29 } 30 return 0; 31 } 32 main() 33 { 34 int i, j, k, num; 35 while(scanf("%d",&n)==1&&n){ 36 memset(from,-1,sizeof(from)); 37 for(i=1;i<=n;i++){ 38 ve[i].clear(); 39 scanf("%d %d %d",&a[i].w,&a[i].l,&a[i].h); 40 } 41 for(i=1;i<=n;i++){ 42 for(j=1;j<=n;j++){ 43 if(a[j].l>a[i].l&&a[j].w>a[i].w&&a[j].h>a[i].h){ 44 ve[j].push_back(i); 45 } 46 } 47 } 48 num=0; 49 for(i=1;i<=n;i++){ 50 memset(visited,0,sizeof(visited)); 51 if(mark(i)) 52 num++; 53 } 54 printf("%d\n",n-num); 55 } 56 }
HDU 4160 最小路径覆盖,布布扣,bubuko.com