HDU_1533 Going Home(最優匹配) 解題報告

轉載請注明出自cxb:http://blog.csdn.net/cxb569262726

題目鏈接:http://acm.hdu.edu.cn/showproblem.php?pid=1533


說實話,這個題目剛開始還真看不出是完備匹配下的最大權匹配(當然,這個也可以用網絡流做。(應該是添加源點、匯點,源點到每個m的距離取m到所有H中最小的那個(用一個大數減掉后就是最大的)匯點到每個H的距離類似,然后求最大流) 有空再試著做一下吧,空說無益)。 我是在圖論500題里看到的,在網絡流基礎題里面。一開始想不出這個怎么流! 后面網上查這個是二分圖最優匹配。于是昨天花幾個小時看了相關資料,寫了個比這題更水的 HDU2255。 今天寫這題的時候明顯輕松了。而且還想到用網絡流的做法。發現網絡流和二分匹配還是有聯系的。


下面直接貼代碼吧,看不懂的可以先看http://blog.csdn.net/cxb569262726/article/details/7871313:


#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<cstdlib>
#include<climits>
#define MAXN 105
using namespace std;int n,m,numm,numh;
int map[MAXN][MAXN],lx[MAXN],ly[MAXN],vx[MAXN],vy[MAXN],matchy_x[MAXN];
char s[MAXN][MAXN];
int abs(int a){return a<0?-a:a;}bool hungary(int u)
{int i;vx[u]=1;for(i=0;i<numm;i++){if(vy[i] || map[u][i]!=lx[u]+ly[i]) continue;vy[i]=1;if(matchy_x[i]==-1 || hungary(matchy_x[i])){matchy_x[i]=u;return  1;}}return 0;
}void EK_match()
{int i,j;for(i=0;i<numm;i++){int maxx=0;for(j=0;j<numh;j++)if(map[i][j]>maxx) maxx=map[i][j];lx[i]=maxx;}for(i=0;i<numm;i++){while(1){memset(vx,0,sizeof(vx));memset(vy,0,sizeof(vy));if(hungary(i))break;else{int temp=INT_MAX;for(j=0;j<numm;j++) if(vx[j])for(int k=0;k<numh;k++) if(!vy[k] && temp>lx[j]+ly[k]-map[j][k])temp=lx[j]+ly[k]-map[j][k];for(j=0;j<numm;j++){if(vx[j]) lx[j]-=temp;if(vy[j]) ly[j]+=temp;}}}}
}int main()
{int i,j;while(scanf("%d%d",&n,&m)!=EOF && (n||m)){for(i=0;i<n;i++) scanf("%s",s[i]);numm=0;for(i=0;i<n;i++){for(j=0;j<m;j++){if(s[i][j]=='m'){numh=0;for(int k=0;k<n;k++){for(int l=0;l<m;l++){if(s[k][l]=='H')map[numm][numh++]=300-(abs(i-k)+abs(j-l));}}numm++;}}}memset(matchy_x,-1,sizeof(matchy_x));EK_match();int ans=0;for(i=0;i<numm;i++)ans+=(300-map[matchy_x[i]][i]);printf("%d\n",ans);}return 0;
}

?以下是slack數組優化的:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<cstdlib>
#include<climits>
#define MAXN 105
using namespace std;int n,m,numm,numh;
int map[MAXN][MAXN],lx[MAXN],ly[MAXN],vx[MAXN],vy[MAXN],matchy_x[MAXN],slack[MAXN];
char s[MAXN][MAXN];
int abs(int a){return a<0?-a:a;}
int min(int a,int b) {return a<b?a:b;}bool hungary(int u)
{int i;vx[u]=1;for(i=0;i<numm;i++){if(vy[i]) continue;if(map[u][i]==lx[u]+ly[i]){vy[i]=1;if(matchy_x[i]==-1 || hungary(matchy_x[i])){matchy_x[i]=u;return  1;}}else slack[i]=min(slack[i],lx[u]+ly[i]-map[u][i]);}return 0;
}void EK_match()
{int i,j;for(i=0;i<numm;i++){int maxx=0;for(j=0;j<numh;j++)if(map[i][j]>maxx) maxx=map[i][j];lx[i]=maxx;}for(i=0;i<numm;i++){memset(slack,127,sizeof(slack));while(1){memset(vx,0,sizeof(vx));memset(vy,0,sizeof(vy));if(hungary(i))break;else{int temp=INT_MAX;for(j=0;j<numm;j++) if(!vy[j])if(temp>slack[j])   temp=slack[j];for(j=0;j<numm;j++){if(vx[j]) lx[j]-=temp;if(vy[j]) ly[j]+=temp;else slack[j]-=temp;}}}}
}int main()
{int i,j;while(scanf("%d%d",&n,&m)!=EOF && (n||m)){for(i=0;i<n;i++)scanf("%s",s[i]);numm=0;for(i=0;i<n;i++){for(j=0;j<m;j++){if(s[i][j]=='m'){numh=0;for(int k=0;k<n;k++){for(int l=0;l<m;l++){if(s[k][l]=='H')map[numm][numh++]=300-(abs(i-k)+abs(j-l));}}numm++;}}}memset(matchy_x,-1,sizeof(matchy_x));EK_match();int ans=0;for(i=0;i<numm;i++)ans+=(300-map[matchy_x[i]][i]);printf("%d\n",ans);}return 0;
}




?ok,睡覺吧。。明天比賽~~~ ?Good ? luck!

轉載于:https://www.cnblogs.com/javaspring/archive/2012/08/16/2656035.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:
http://www.pswp.cn/news/377269.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/377269.shtml
英文地址,請注明出處:http://en.pswp.cn/news/377269.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

c#中 uint_C#中的uint關鍵字

c#中 uintC&#xff03;uint關鍵字 (C# uint keyword) In C#, uint is a keyword which is used to declare a variable that can store an integral type of value (unsigned integer) the range of 0 to 4,294,967,295. uint keyword is an alias of System.UInt32. 在C&…

《MySQL——事務》

目錄事務的必要性MySQL中如何控制事務手動開啟事務事務的四大特征事務的四大特征事務開啟方式事務手動提交與手動回滾事務的隔離性臟讀現象不可重復讀現象幻讀現象串行化一些補充使用長事務的弊病commit work and chain的語法是做什么用的?怎么查詢各個表中的長事務&#xff1…

運行在TQ2440開發板上以及X86平臺上的linux內核編譯

一、運行在TQ2440開發板上的linux內核編譯 1、獲取源碼并解壓 直接使用天嵌移植好的“linux-2.6.30.4_20100531.tar.bz2”源碼包。 解壓&#xff08;天嵌默認解壓到/opt/EmbedSky/linux-2.6.30.4/中&#xff09; tar xvjf linux-2.6.30.4_20100531.tar.bz2 -C / 2、獲取默認配置…

ArcCatalog ArcMap打不開

原來是因為&#xff1a; 連接了電信的無線網卡 關掉即可 啟動ArcCatalog之后再開啟無線網卡 沒問題&#xff01;轉載于:https://www.cnblogs.com/ccjcjc/archive/2012/08/21/2649867.html

Python熊貓– GroupBy

Python熊貓– GroupBy (Python Pandas – GroupBy) GroupBy method can be used to work on group rows of data together and call aggregate functions. It allows to group together rows based off of a column and perform an aggregate function on them. GroupBy方法可用…

MySQL索引底層原理理解以及常見問題總結

目錄二叉查找樹為索引紅黑樹為索引B樹作為索引B樹作為索引MyISAM存儲引擎索引實現InnoDB存儲引擎索引實現常見問題聚集索引與非聚集索引InnoDB基于主鍵索引和普通索引的查詢有什么區別&#xff1f;InnoDB主鍵索引為何是整型的自增主鍵何時使用業務字段作為主鍵呢&#xff1f;哈…

Spring之HibernateTemplate 和HibernateDaoSupport

spring提供訪問數據庫的有三種方式&#xff1a; HibernateDaoSupport HibernateTemplate&#xff08;推薦使用&#xff09; jdbcTemplate(我們一般不用&#xff09; 類所在包&#xff1a; HibernateTemplate&#xff1a;org.springframework.orm.hibernate3.HibernateTemplate …

JDOJ-重建二叉樹

這是一道面試題&#xff0c;可以說是數據結構中的基礎題了&#xff0c;由先序遍歷以及中序遍歷生成一棵樹&#xff0c;然后輸出后序遍歷。 一個遞歸函數傳遞5個參數&#xff0c;頂點編號&#xff0c;先序左右區間&#xff0c;中序左右區間&#xff0c;每次進行區間長度判定&…

des算法密碼多長_密碼學中的多個DES

des算法密碼多長This is a DES that was susceptible to attacks due to tremendous advances in computer hardware in cryptography. Hence, it was a very complex or competent algorithm it would be feasible to reuse DES rather than writing an of cryptography. 由于…

《MySQL——索引筆記》

目錄回表覆蓋索引最左前綴原則聯合索引的時候&#xff0c;如何安排索引內的字段順序&#xff1f;索引下推重建索引問題聯合主鍵索引和 InnoDB 索引組織表問題in與between的區別回表 回到主鍵索引樹搜索的過程&#xff0c;我們稱為回表。 覆蓋索引 覆蓋索引就是在這次的查詢中…

計算凸多邊形面積的算法

1. 思路&#xff1a; 可以將凸多邊形&#xff08;邊數n > 3&#xff09;劃分為 (n - 2) 個三角形&#xff0c;分別運用向量叉積計算每個三角形的面積&#xff0c;最后累加各個三角形的面積就是多邊形的面積。 2. 求多邊形面積的算法模板&#xff1a;   定義點的結構體 str…

Windows CE開發常見問題解答

轉自&#xff1a; http://blog.csdn.net/slyzhang/article/details/6110490 1.怎樣在一個控件獲得焦點時打開軟鍵盤&#xff1f;比如一個EditBox獲得焦點后&#xff0c;這個時候自動打開軟鍵盤&#xff0c;這樣可以方便用戶輸入——SIPINFO、SHSIPINFO、SIPSETINFO、SIPGETINFO…

Julia中的supertype()函數

Julia| supertype()函數 (Julia | supertype() function) supertype() function is a library function in Julia programming language, it is used to get the concrete supertype of the given type (data type). supertype()函數是Julia編程語言中的庫函數&#xff0c;用于…

《操作系統知識點整理》

目錄進程與線程比較多線程同步與互斥生產者與消費者哲學家就餐問題讀者寫者問題進程間通信管道消息隊列共享內存信號量信號Socket鎖互斥鎖與自旋鎖讀寫鎖樂觀鎖與悲觀鎖死鎖進程與線程比較 進程是資源&#xff08;包括內存、打開的文件等&#xff09;分配的單位&#xff0c;線…

for,foreach,iterator的用法和區別

相同點&#xff1a; 三個都可以用來遍歷數組和集合不同點&#xff1a;1.形式差別 for的形式是 for&#xff08;int i0;i<arr.size();i&#xff09;{...} foreach的形式是 for&#xff08;int i&…

和菜鳥一起學linux總線驅動之初識spi驅動主要結構

既然知道了協議了&#xff0c;那么就可以開始去瞧瞧linux kenerl中的spi的驅動代碼了&#xff0c;代碼中有很多的結構體&#xff0c;還是對主要的結構體先做個了解吧&#xff0c;那樣才可以很好的理解驅動。主要是include/linux/spi.h 首先是SPI的主機和從機通信接口&#xff0…

操作系統大內核和微內核_操作系統中的內核

操作系統大內核和微內核A Kernel is the central component of an Operating System. The Kernel is also said to be the heart of the Operating System. It is responsible for managing all the processes, memory, files, etc. The Kernel functions at the lowest level …

《MySQL——鎖》

全局鎖是什么&#xff1f;全局鎖有什么用&#xff1f;全局鎖怎么用&#xff1f; 全局鎖主要用在邏輯備份過程中&#xff0c;對于InnoDB 引擎的庫&#xff0c;使用–single-transaction; MySQL 提供了一個加全局讀鎖的方法&#xff0c;命令是 Flush tables with read lock (FTW…

搜索引擎Constellio及Google Search Appliances connectors

做搜索產品的時候發現國外一個同類型的產品contellio&#xff0c;發現功能比較強大&#xff0c;先記錄下來 貌似可以添加文檔 網站 以及數據庫等不同類型的數據源 http://wiki.constellio.com/index.php/Main_Page http://www.constellio.com/ http://www.constellio.com htt…

dig下載_DIG的完整形式是什么?

dig下載DIG&#xff1a;副監察長 (DIG: Deputy Inspector General) DIG is an abbreviation of the Deputy Inspector General. It is a high-level position in the Indian Police Service. The officers who already offered service on Senior Superintendent of Police (SS…