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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

POJ2195 Going Home 最小费用最大流

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

POJ 2195 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間房子,每人進入一個房子,求最小的總距離
分析:

關鍵在于建圖:

源點為0,匯點為2*n+1,n為房子個數(房子數等于人數);

源點到每個人建立一條容量為1,費用為0的邊;

每個人到每個房子建立一條容量為1,費用為兩者距離的邊;

每個人到匯點建立一條容量為1,費用為0的邊;

然后跑一遍最小費用最大流即可

#include<stdio.h> #include<iostream> #include<string.h> #include<queue> #include<cstdio> #include<string> #include<math.h> #include<algorithm> #include<map> #include<set> #include<stack> #define mod 998244353 #define INF 0x3f3f3f3f #define eps 1e-6 using namespace std; typedef long long ll; #define MAXN 1003 #define MAXM 40004 //最小費用最大流 struct Edge{int to,next;int flow,cost,cap; }edge[MAXM]; int tol,head[MAXN]; void init() {tol=0;memset(head,-1,sizeof head); } void addEdge(int u,int v,int cap,int cost){edge[tol].to=v;edge[tol].cap=cap;edge[tol].cost=cost;edge[tol].flow=0;edge[tol].next=head[u];head[u]=tol++;edge[tol].to=u;edge[tol].cap=0;edge[tol].cost=-cost;edge[tol].flow=0;edge[tol].next=head[v];head[v]=tol++; }bool inq[MAXN];//標記是否點是否在隊列 int dis[MAXN];//最短距離 int pre[MAXN];//記錄路徑 int q[MAXN*10];//隊列 //單位費用可能是負值,所以用SPFA bool spfa(int st,int en) {memset(inq,0,sizeof inq);memset(dis,INF,sizeof dis);memset(pre,-1,sizeof pre);int rear=0,front=0;dis[st]=0;inq[st]=true;q[front++]=st;while(rear<front){int u=q[rear++];inq[u]=false;for(int e=head[u];e!=-1;e=edge[e].next){int v=edge[e].to;if(edge[e].cap>edge[e].flow&&dis[v]>dis[u]+edge[e].cost){dis[v]=dis[u]+edge[e].cost;pre[v]=e;//表示邊e-->v,e就是v的前驅if(!inq[v])inq[v]=true,q[front++]=v;}}}return pre[en]!=-1; } int minCostMaxFlow(int st,int en,int &cost,int &flow) {//如果能找到從源點到匯點的最短路,說明還沒有達到最小費用最大流while(spfa(st,en)){int Min=INF;//最小殘余流量//沿著當前路徑返回for(int i=pre[en];i!=-1;i=pre[edge[i^1].to]){int rem=edge[i].cap-edge[i].flow;Min=Min>rem?rem:Min;}for(int i=pre[en];i!=-1;i=pre[edge[i^1].to]){edge[i].flow+=Min;//正向邊添加殘余流量edge[i^1].flow-=Min;//反向邊減少殘余流量cost+=Min*edge[i].cost;}flow+=Min;} } //以上為最小費用最大流模板int n,m; struct point{int x,y; }house[MAXN],men[MAXN]; char str[110]; int main() {while(scanf("%d%d",&n,&m)&&n&&m){init();int houseNum=1,menNum=1;for(int i=0;i<n;i++){scanf("%s",str);for(int j=0;j<m;j++){if(str[j]=='H')house[houseNum].x=i,house[houseNum].y=j,houseNum++;if(str[j]=='m')men[menNum].x=i,men[menNum].y=j,menNum++;}}int st=0;int en=houseNum+menNum-1;for(int i=1;i<menNum;i++){for(int j=1;j<houseNum;j++){int w=abs(men[i].x-house[j].x)+abs(men[i].y-house[j].y);addEdge(i,menNum+j-1,1,w);}}for(int i=1;i<menNum;i++){addEdge(0,i,1,0);addEdge(menNum+i-1,en,1,0);}int cost=0,flow=0;minCostMaxFlow(st,en,cost,flow);printf("%d\n",cost);} }

?

總結

以上是生活随笔為你收集整理的POJ2195 Going Home 最小费用最大流的全部內容,希望文章能夠幫你解決所遇到的問題。

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

主站蜘蛛池模板: 玖玖视频国产 | 激情综合亚洲 | 婷婷六月天 | 一个色综合网 | 99热视 | 99久久人妻精品免费二区 | 国产精品九九热 | av之家在线 | av中文字幕在线免费观看 | 亚洲国产精彩视频 | аⅴ资源中文在线天堂 | 狂躁美女大bbbbbb黑人 | 男人的天堂视频在线观看 | 日本一本不卡 | 日批视频免费播放 | 丰满大乳奶做爰ⅹxx视频 | 国产又大又黑又粗免费视频 | 自拍亚洲欧美 | jjzz在线| 动漫美女视频 | 精品久久久久久久久久久久久久久久久 | 樱花动漫无圣光 | 福利网址在线观看 | 日韩一区中文 | 亚洲高清久久久 | 伊人网欧美| www.五月婷婷| 天天舔天天干 | 亚洲国产精品激情在线观看 | 国产精品资源网站 | 自拍1页 | 瑟瑟在线观看 | 激情综合网激情 | 日本午夜网站 | 国产又大又粗又硬 | www.四虎com| 欧美成人精品三级网站 | 免费av不卡| 一级成人黄色片 | 日本公妇乱淫免费视频一区三区 | 在线看的免费网站 | 91亚洲精品久久久蜜桃 | 美女又爽又黄视频 | 国产亚洲天堂 | 色呦呦在线免费观看 | 亚洲毛片在线免费观看 | av片在线播放 | 瑟瑟视频免费看 | 凹凸视频一区二区 | 欧美一级视频免费观看 | 啪网站| 精品亚洲一区二区三区 | 成人av片在线观看 | 福利在线播放 | 中文字幕乱码中文字幕 | 日本爽爽爽 | 青青草超碰 | 日本 奴役 捆绑 受虐狂xxxx | 色屁屁 | 九热在线视频 | 素人一区 | www.插插插.com | 色呦呦视频在线 | 久久99久久99精品免观看软件 | 色狠狠av| 国产欧美一区二区三区视频在线观看 | 久久久久久久久久久97 | 天天躁日日躁狠狠躁av麻豆男男 | 国产91啪| 久福利| 邻居少妇张开腿让我爽了在线观看 | 噜噜噜久久| 国产成人无码久久久精品天美传媒 | 亚洲视频免费播放 | 麻豆久久久午夜一区二区 | 91色漫| 美女网站在线看 | 性视频免费 | 女人高潮被爽到呻吟在线观看 | 日产精品久久久一区二区 | 四虎视频国产精品免费入口 | 国产欧美日韩三级 | 亚州综合网 | 亚洲色p | 国产一区二区三区亚洲 | 九九久久精品 | 五月av| 伊人久久99 | 国产香蕉视频在线观看 | 香蕉福利| 成人3d动漫在线观看 | 看黄网站在线观看 | 久久日本视频 | 日本成人综合 | 黄色污污网站在线观看 | 欧美18—19性高清hd4k | 日韩成人免费在线观看 | 91成年版| 麻豆视频在线免费观看 |