Going Home
Time limit:10000/5000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 5539 Accepted Submission (s): 2907
Problem Descriptionon A grid map There is n little men and N houses. In each unit time, every little mans can move one unit step, either horizontally, or vertically, to a adjacent point. For each little mans, you need to pay a $ fee for every step he moves, until he enters a house. The task is complicated with the restriction, each house can accommodate only one little man.
Your task is to compute the minimum amount of money you need to pay in order to send these n little men into those n Diffe Rent houses. The input is a map of the scenario, a '. ' means an empty space, an ' H ' represents a house on that point, and am ' m ' indica TES There is a little man on this point.
You can think of all the the grid map as a quite large square, so it can hold n little men at the same time; Also, it is the okay if a little man steps on a grid with a house without entering this house.
Inputthere is one or more test cases in the input. Each case starts with a line giving-integers n and m, where N is the number of rows of the map, and M are the number of Columns. The rest of the input would be N lines describing the map. Assume both N and M are between 2 and inclusive. There would be the same number of ' H ' s and ' M's on the map; And there'll is at the most houses. Input would terminate with 0 0 for N and M.
Outputfor each test case, output one line with the single integer, which are the minimum amount, in dollars, your need to PA Y.
Sample Input2 2.mh.5 5HH.. m...............mm. H7 8...H ..... H....... H....mmmhmmmm ... H....... H....... H.... 0 0
Sample Output21028
/** Title: hdu1533 going Home km algorithm solves the minimum weight perfect match link:http://acm.hdu.edu.cn/showproblem.php?pid=1533Test instructions: LV idea: Best match (maximum weight perfect match) km algorithm if the minimum weight is the perfect match, then the ownership value to the opposite number, then the maximum weight, the output of the maximum right of the opposite number can be. */#include<iostream>#include<cstring>#include<cstdio>#include<vector>#include<cmath>#include<queue>using namespacestd;Const intMAXN = the;Const intINF =0x3f3f3f3f;intLOVE[MAXN][MAXN];//record the goodwill of every sister and every boyintEX_GIRL[MAXN];//the expectations of every sisterintEX_BOY[MAXN];//the expectations of every boyBOOLVIS_GIRL[MAXN];//record every match of the match GirlBOOLVIS_BOY[MAXN];//keep track of each match, match the boys.intMATCH[MAXN];//record each boy's match to the sister, if not, 1.intSLACK[MAXN];//keep track of the number of expectations that every man needs at least if he can be attracted to a sisterintN//left vertex number = right vertex number =n;BOOLDfsintgirl) {Vis_girl[girl]=true; for(intBoy =0; Boy < N; ++Boy ) { if(Vis_boy[boy])Continue;//each round matches each boy only try once intGap = Ex_girl[girl] + ex_boy[boy]-Love[girl][boy]; if(Gap = =0) {//If the requirements are metVis_boy[boy] =true; if(Match[boy] = =-1|| DFS (Match[boy])) {//find a guy who doesn't have a match, or the boy's sister can find someone else .Match[boy] =girl; return true; } } Else{Slack[boy]= Min (Slack[boy], GAP);//slack can be understood as the boy who wants to get a girl's heart. How much will it take to have a minimum spare tire appearance "cover your face } } return false;}intKM () {memset (match,-1,sizeofMatch);//Initially, every guy doesn't have a matching girl.memset (Ex_boy,0,sizeofEx_boy);//the initial expectation for each boy is 0.//The initial expectation of each girl is the greatest affection of the boy who is connected to her . for(inti =0; i < N; ++i) {Ex_girl[i]= love[i][0]; for(intj =1; J < N; ++j) {Ex_girl[i]=Max (Ex_girl[i], love[i][j]); } } //try to solve a problem for every girl for(inti =0; i < N; ++i) {Fill (slack, slack+ N, INF);//because to take the minimum value to initialize to infinity while(1) { //The way to solve the problem for each girl is to reduce the expectation if it is not found.//keep track of whether the boys and girls are trying to match each round .memset (Vis_girl,false,sizeofvis_girl); memset (Vis_boy,false,sizeofVis_boy); if(Dfs (i)) Break;//find a destination to exit//If you can't find it, lower your expectations .//minimum expected value to reduce intD =INF; for(intj =0; J < N; ++j)if(!vis_boy[j]) d =min (d, slack[j]); for(intj =0; J < N; ++j) {//all girls who have visited lower their expectations if(Vis_girl[j]) ex_girl[j]-=D; //all the boys visited increased their expectations. if(Vis_boy[j]) ex_boy[j] + =D; //the boy who has not visited because the expectations of the girl lower, the distance to get a girl fell in a step! ElseSLACK[J]-=D; } } } //match completion to find out all pairs of goodwill and intres =0; for(inti =0; i < N; ++i) Res+=love[Match[i]][i]; returnRes;}intF[MAXN][MAXN];CharMp[maxn][maxn];typedef pair<int,int>P;vector<P>A, B;intDisintIintj) { returnABS (A[i].first-b[j].first) +abs (a[i].second-b[j].second);}intMain () {intN, M; while(SCANF ("%d%d", &n,&m) = =2) {//N External Variables if(n==0&&m==0) Break; A.clear (), b.clear (); for(inti =0; I < n; i++) {scanf ("%s", Mp[i]); for(intj =0; J < M; J + +){ if(mp[i][j]=='m') {a.push_back (P (i,j)); } if(mp[i][j]=='H') {b.push_back (P (i,j)); } }} N=a.size (); for(inti =0; i < N; ++i) for(intj =0; J < N; ++j) Love[i][j]= -dis (i,j); printf ("%d\n", -KM ()); } return 0;}
hdu1533 going Home km algorithm solves the smallest right perfect match