hdu 4160 Dolls 匈牙利算法求最大匹配
生活随笔
收集整理的這篇文章主要介紹了
hdu 4160 Dolls 匈牙利算法求最大匹配
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Dolls
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Time Limit: 2000/1000 MS (Java/Others)????Memory Limit: 32768/32768 K (Java/Others)題目鏈接
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 剛見到這個題,想到了以前做過的一個題Wooden Sticks,所以第一想法就是用貪心做,寫好以后樣例也過了,但是提交了兩次都是 wrong answer,卻找不到出錯誤原因。后來想了下,決定換用另一種方法,因為剛學過二分圖,就試著用二分圖來做。二分圖最主要的就是如何建圖。建圖時,如果兩個dools可以嵌套,則說明這兩個dool有關系,所以就給這兩個dool的編號連上一條邊。求外面最少的doll,就相當于轉化成了求最小點擊覆蓋,我們只需要求出最大匹配,用總的個數減去最大匹配,問題就解決了。AC代碼:/*用匈牙利算法二分圖求最大匹配*/ #include<stdio.h> #include<string.h> #include<vector> #include<algorithm> using namespace std; vector<int> vec[505]; int flag[505],vis[505]; struct doll {int li,wi,hi; }a[505];//定義結構體數組 bool comp(doll a1,doll a2) {if(a1.wi==a2.wi)return a1.li<a2.li; //長度從小到大排序elsereturn a1.wi<a2.wi; //寬度從小到大排序if(a1.li==a2.li)return a1.hi<a2.hi; //高度從小到大排序elsereturn a1.li<a2.li; } bool find(int a) // 尋找最大匹配 {int i;for(i=0;i<vec[a].size();i++){if(!vis[vec[a][i]]){vis[vec[a][i]]=1;if(flag[vec[a][i]]==0||find(flag[vec[a][i]])){flag[vec[a][i]]=a;return true;}}}return false; } int main() {int n,i,j,count;while(scanf("%d",&n)!=EOF&&n!=0){for(i=0;i<n;i++)scanf("%d%d%d",&a[i].wi,&a[i].li,&a[i].hi);sort(a,a+n,comp);memset(vec,0,sizeof(vec));memset(flag,0,sizeof(flag)); //數組清零count=0; //記錄最大匹配for(i=0;i<n;i++)for(j=i+1;j<n;j++){if(a[j].li>a[i].li&&a[j].wi>a[i].wi&&a[j].hi>a[i].hi) //若可以嵌套,說明有邊連接vec[i].push_back(j); //建立關系}for(i=0;i<n;i++){memset(vis,0,sizeof(vis)); //記得每次都要清零if(find(i))count++; //找到新的匹配,數量加1}printf("%d\n",n-count); //輸出不相關即不能嵌套的doll數量}return 0; }
用匈牙利算法求最大匹配時,一般使用鄰接矩陣來求,即用一個map[i][j]的二維數組,但是如果有用的邊較少的話,我們就浪費了很多時間去遍歷那些無用的邊,所以用鄰接表來優化,鄰接表用STL中的vector來實現。具體怎么使用我就不多說了。
總結
以上是生活随笔為你收集整理的hdu 4160 Dolls 匈牙利算法求最大匹配的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 程序员自救指南:一不小心删库删表怎么办?
- 下一篇: NYOJ 608 畅通工程 并查集