HDU 4160 Dolls (最小路径覆盖=顶点数-最大匹配数)

Dolls

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


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个布娃娃,可以用长宽高代表他们的特征,如果一个布娃娃的长宽高都比另一个小,那么这个布娃娃可以放到另一个的里面,问你求把布娃娃放置到另一个里,剩下的最少娃娃数量

 
思路: 最小路径覆盖=顶点数-最大匹配数
import java.io.*;
import java.util.*;
public class Main {
	int n,MAX=10010;
	int[][] map;
	int[] link=new int[MAX];
	boolean[] mark=new boolean[MAX];
	public static void main(String[] args) {
		new Main().work();
	}
	
	
	void work(){
		Scanner sc=new Scanner(new BufferedInputStream(System.in));
		while(sc.hasNext()){
			n=sc.nextInt();
			if(n==0) break;
			Node node[]=new Node[n];
			for(int i=0;i<n;i++){
				int wi=sc.nextInt();
				int li=sc.nextInt();
				int hi=sc.nextInt();
				node[i]=new Node(wi,li,hi);
			}
			Arrays.sort(node);
			map=new int[500][508];
			for(int i=0;i<n;i++){
				for(int j=0;j<n;j++){
					if(node[i].wi<node[j].wi&&node[i].li<node[j].li&&node[i].hi<node[j].hi){
						map[i][j]=1;
					}
				}
			}
			hungary();
		}
	}
	
	
	void hungary(){
		int ans=0;
		Arrays.fill(link, 0);
		for(int i=0;i<n;i++){
			Arrays.fill(mark, false);
			if(DFS(i))
				ans++;
		}
		System.out.println(n-ans);
	}
	
	
	boolean DFS(int x){
		for(int i=0;i<n;i++){
			if(map[x][i]==1&&!mark[i]){
				mark[i]=true;
				if(link[i]==0||DFS(link[i])){
					link[i]=x;
					return true;
				}
			}
		}
		return false;
	}
	
	
	class Node implements Comparable<Node>{
		int wi;
		int li;
		int hi;
		Node(int wi,int li,int hi){
			this.wi=wi;
			this.li=li;
			this.hi=hi;
		}
		public int compareTo(Node o) {
			if(this.wi>o.wi) return 1;
			if((this.wi==o.wi)&&(this.li>o.li)) return 1;
			if((this.wi==o.wi)&&(this.li==o.li)&&(this.hi>o.hi))
				return 1;
			return -1;
		}
	}
}
原文地址:https://www.cnblogs.com/james1207/p/3297185.html