POJ 2195 Going Home 二分图的最大权匹配
POJ2195 Going Home 最小費(fèi)用最大流
Going Home
| Time Limit:?1000MS | ? | Memory Limit:?65536K |
| Total Submissions:?25567 | ? | Accepted:?12838 |
Description
On a grid map there are n little men and n houses. In each unit time, every little man can move one unit step, either horizontally, or vertically, to an adjacent point. For each little man, you need to pay a $1 travel fee for every step he moves, until he enters a house. The task is complicated with the restriction that 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 different 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' indicates there is a little man on that point.?
You can think of each point on the grid map as a quite large square, so it can hold n little men at the same time; also, it is okay if a little man steps on a grid with a house without entering that house.
Input
There are one or more test cases in the input. Each case starts with a line giving two integers N and M, where N is the number of rows of the map, and M is the number of columns. The rest of the input will be N lines describing the map. You may assume both N and M are between 2 and 100, inclusive. There will be the same number of 'H's and 'm's on the map; and there will be at most 100 houses. Input will terminate with 0 0 for N and M.
Output
For each test case, output one line with the single integer, which is the minimum amount, in dollars, you need to pay.
Sample Input
2 2 .m H. 5 5 HH..m ..... ..... ..... mm..H 7 8 ...H.... ...H.... ...H.... mmmHmmmm ...H.... ...H.... ...H.... 0 0Sample Output
2 10 28Source
Pacific Northwest 2004
題意:
有k個(gè)人,k間房子,每人進(jìn)入一個(gè)房子,求最小的總距離
分析:
對(duì)每條邊取相反數(shù),然后得到的結(jié)果再取相反數(shù),就能得到最小權(quán)匹配
Java
import java.math.*; import java.util.*;public class Main {static int inf=(int)1e8+7;static int maxn=110;static int n;//n個(gè)球,n個(gè)洞static int[][] w=new int[maxn][maxn];//w[i][j]表示i到j(luò)的權(quán)值static int[] lx=new int[maxn];static int[] ly=new int[maxn];static int[] link=new int[maxn];static int[] slack=new int[maxn];static boolean[] visx=new boolean[maxn];static boolean[] visy=new boolean[maxn];static boolean dfs(int x) {visx[x]=true;for(int y=1;y<=n;y++) {//if(visy[y]) continue;int t=lx[x]+ly[y]-w[x][y];if(!visy[y]&&t==0) {visy[y]=true;if(link[y]==0||dfs(link[y])) {link[y]=x;return true;}}else if(!visy[y]&&lx[x]+ly[y]>w[x][y])slack[y]=Math.min(slack[y], lx[x]+ly[y]-w[x][y]);}return false;}static int km() {for(int i=0;i<maxn;i++) {lx[i]=-inf;ly[i]=0;link[i]=0;}for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)lx[i]=Math.max(lx[i], w[i][j]);for(int i=1;i<=n;i++) {for(int j=1;j<=n;j++) slack[j]=inf;while(true) {for(int k=1;k<maxn;k++) {visx[k]=false;visy[k]=false;}if(dfs(i)) break;int d=inf;for(int k=1;k<=n;k++)if(!visy[k]&&d>slack[k])d=slack[k];if(d==-1) return -1;// no matchingfor(int k=1;k<=n;k++) {if(visx[k]) lx[k]-=d;if(visy[k]) ly[k]+=d;}}}int ans=0;for(int i=1;i<=n;i++)ans+=w[link[i]][i];return ans;}public static void main (String[] args) {Scanner cin=new Scanner(System.in);while(cin.hasNext()) {int[] ball_x=new int[maxn];int[] ball_y=new int[maxn];int[] hole_x=new int[maxn];int[] hole_y=new int[maxn];int bk=1,hk=1;int M=cin.nextInt();int N=cin.nextInt();String str="";if(M==0&&N==0) break;for(int i=0;i<M;i++) {str=cin.next();for(int j=0;j<N;j++) {if(str.charAt(j)=='m') {ball_x[bk]=i;ball_y[bk]=j;bk++;}else if(str.charAt(j)=='H') {hole_x[hk]=i;hole_y[hk]=j;hk++;}}}n=bk-1;//System.out.println(n);for(int i=1;i<=n;i++) {for(int j=1;j<=n;j++) {w[i][j]=-Math.abs(ball_x[i]-hole_x[j])-Math.abs(ball_y[i]-hole_y[j]);//System.out.print(-w[i][j]+" ");}//System.out.println();}System.out.println(-km());}cin.close();} }?
總結(jié)
以上是生活随笔為你收集整理的POJ 2195 Going Home 二分图的最大权匹配的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 二分图的最大匹配 匈牙利算法
- 下一篇: 二分图最大权匹配 KM算法