日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

POJ 2195 Going Home 二分图的最大权匹配

發布時間:2024/10/6 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 POJ 2195 Going Home 二分图的最大权匹配 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

POJ2195 Going Home 最小費用最大流

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 0

Sample Output

2 10 28

Source

Pacific Northwest 2004

題意:

有k個人,k間房子,每人進入一個房子,求最小的總距離
分析:
對每條邊取相反數,然后得到的結果再取相反數,就能得到最小權匹配

#include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define INF 0x3f3f3f3f #define MAXN 105using namespace std;int n;//二分圖頂點集的大小 int a[MAXN][MAXN];//a[i][j]表示i到j的權值 int vx[MAXN],vy[MAXN]; int lx[MAXN],ly[MAXN];//同時調節兩個數組,使得權值和最大 int pre[MAXN];//記錄頂點集n2中點x在頂點集n1中所匹配的點x的編號 int slack[MAXN];//松弛操作 int N,M;//N行M列 int cnth,cntm; struct point{int x,y; }tmph[MAXN],tmpm[MAXN]; char st[MAXN][MAXN];bool dfs(int index) {vx[index]=1;for(int i=1;i<=n;i++){if(!vy[i]&&lx[index]+ly[i]==a[index][i]){//這個條件下使用匈牙利算法vy[i]=1;//這個房子沒人住或者可以讓住這個房子的人找另外的房子if(!pre[i]||dfs(pre[i])){pre[i]=index;return 1;}}else if(!vy[i]&&lx[index]+ly[i]>a[index][i])slack[i]=min(slack[i], lx[index]+ly[i]-a[index][i]);}return 0; }int KM() {memset(ly,0,sizeof(ly));//首先把每個居民出錢最多的那個房子給他for(int i=1;i<=n;i++){lx[i]=-INF;for(int j=1;j<=n;j++)lx[i]=max(lx[i],a[i][j]);}int ans=0,d;memset(pre,0,sizeof(pre));//給第i位居民分配房子for(int i=1;i<=n;i++){memset(slack,INF,sizeof(slack));while(1){memset(vx,0,sizeof(vx));memset(vy,0,sizeof(vy));if(dfs(i))break;d=INF;for(int i=1;i<=n;i++)if(!vy[i])d=min(d,slack[i]);//找到最小松弛量if(d==INF) return -1; //no matchingfor(int j=1;j<=n;j++){if(vx[j]) lx[j]-=d;if(vy[j]) ly[j]+=d;}}}for(int i=1;i<=n;i++)ans+=a[pre[i]][i];return ans; }int main() {while(~scanf("%d %d", &N, &M)){if(!N&&!M)break;cnth=cntm=0;getchar();for(int i=1;i<=N;i++){for(int j=1;j<=M;j++){scanf("%c",&st[i][j]);if(st[i][j]=='m')cntm++,tmpm[cntm].x=i, tmpm[cntm].y=j;if(st[i][j]=='H')cnth++,tmph[cnth].x=i, tmph[cnth].y=j;}getchar();}n=cntm;for(int i=1;i<=cntm;i++)for(int j=1;j<=cnth;j++)a[i][j]=-(abs(tmpm[i].x-tmph[j].x)+abs(tmpm[i].y-tmph[j].y));printf("%d\n",-KM());}return 0; }

Java

import java.math.*; import java.util.*;public class Main {static int inf=(int)1e8+7;static int maxn=110;static int n;//n個球,n個洞static int[][] w=new int[maxn][maxn];//w[i][j]表示i到j的權值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();} }

?

總結

以上是生活随笔為你收集整理的POJ 2195 Going Home 二分图的最大权匹配的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 午夜网页 | 你懂的在线观看网址 | 老熟妇高潮一区二区高清视频 | 综合色综合 | 丰满多毛的大隂户视频 | 青青草原在线免费观看视频 | 国产伦一区二区三区 | 色av中文字幕 | 免费看成人aa片无码视频羞羞网 | 99综合视频 | 成人欧美视频在线观看 | 久精品在线观看 | 日韩专区一区 | 草久久av | 国产日日夜夜 | 欧美黑人xxxⅹ高潮交 | 女儿的朋友在线播放 | 私人影院毛片 | 中字幕视频在线永久在线观看免费 | 久久99久久久久久 | 欧美壮男野外gaytube | 黄色免费毛片 | 欧美体内谢she精2性欧美 | 亚洲电影一区二区三区 | jizjiz中国少妇高潮水多 | 伊人春色在线 | 蜜臀在线观看 | 91麻豆精品国产理伦片在线观看 | 久久最新精品 | 欧美一区免费看 | 国产麻豆精品久久一二三 | 欧美大片xxxx | 亚洲精品国产精品国自产观看 | 精品三级av | 91情侣视频 | wwwww在线观看 | 亚洲天堂123 | 新婚之夜玷污岳丰满少妇在线观看 | 97影视 | 免费簧片在线观看 | 风流老熟女一区二区三区 | 在线中文字幕日韩 | 新91视频在线观看 | 中文字幕5566 | 天天射日日 | 国产www精品 | 欧美精品在线看 | 国产精品美女 | 中文天堂资源在线 | 日韩午夜激情 | 一区二区在线免费观看视频 | 黄色大片免费观看视频 | 久久国产毛片 | 夜夜爽日日澡人人添 | 色爽影院 | 韩国三级在线看 | 欧美女人一区二区 | 国产精品第2页 | 免费不卡的av | 色妞综合网 | 精品一区二区三区av | 国产欧美日韩亚洲 | 国产视频第一区 | 欧美视频一区二区三区四区在线观看 | 青青草97国产精品免费观看 | 欧美三级国产 | 欧美激情二区三区 | 最新中文字幕免费视频 | 国产亚洲电影 | 亚洲精品66 | 蜜桃成人无码区免费视频网站 | 四虎成人精品在永久免费 | 日韩三级精品 | 亚洲男人天堂网站 | 男女做爰猛烈高潮描写 | 国产一区二区三区小说 | 视频免费在线观看 | 日韩激情啪啪 | 亚洲美女啪啪 | 久久国产中文字幕 | 午夜视频成人 | 国产日韩一区二区 | 久视频在线 | 91丨国产丨白丝 | 日韩午夜毛片 | 国产美女三级无套内谢 | 撸撸在线视频 | 男女插插插视频 | 香蕉在线视频播放 | 国产一区二区三区在线免费观看 | 美日韩在线视频 | 奇米色777 | 激情六月天 | 98久久久| 99精品中文字幕 | 日日天天| 欧美不卡在线观看 | 久久中文字幕一区二区 | xxx久久|