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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

【HDU - 1533】Going Home(网络流,二分图最优匹配,KM算法)

發布時間:2023/12/10 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【HDU - 1533】Going Home(网络流,二分图最优匹配,KM算法) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題干:

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

題目大意:

在n*m的矩陣格子里有x個人('m')要走到x個房間('H')里,每個房間里只能放一人,人在目前所在的格子處向上下左右相鄰的格子走一步就花費1美元,問最后全部的人走到房間后最小的花費。

解題報告:

? 直接建圖之后最小費用流。

新建超級源點st,超級匯點ed。

st連向每一個m,流量為1,費用為0。

每一個H連向ed,

新增源點from和匯點to,from->人(w為1,cost為0)

房子->to(w為1,cost為0)

每個人->每間房(w為1,cost為最短路徑的美元花費)

AC代碼:

#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define ll long long using namespace std;const int MAXN = 70000; const int MAXM = 100005; const int INF = 0x3f3f3f3f; struct Edge {int to,next,cap,flow,cost; } edge[MAXM]; int head[MAXN],tol; int pre[MAXN],dis[MAXN]; bool vis[MAXN]; int N;//節點總個數,節點編號從 1 ~ N void init(int n) {N = n;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 spfa(int s,int t) {queue<int>q;for(int i = 1; i <= N; i++) {dis[i] = INF;vis[i] = false;pre[i] = -1;}dis[s] = 0;vis[s] = true;q.push(s);while(!q.empty()) {int u = q.front();q.pop();vis[u] = false;for(int i = head[u]; i !=-1; i = edge[i].next) {int v = edge[i].to;if(edge[i].cap > edge[i].flow && dis[v] > dis[u] + edge[i].cost ) {dis[v] = dis[u] + edge[i].cost;pre[v] = i;if(!vis[v]) {vis[v] = true;q.push(v);}}}}if(pre[t] ==-1)return false;else return true;} //返回的是最大流,cost 存的是最小費用 int minCostMaxflow(int s,int t,int &cost) {int flow = 0;cost = 0;while(spfa(s,t)) {int Min = INF;for(int i = pre[t]; i !=-1; i = pre[edge[i^1].to]) {if(Min > edge[i].cap-edge[i].flow)Min = edge[i].cap-edge[i].flow;}for(int i = pre[t]; i !=-1; i = pre[edge[i^1].to]) {edge[i].flow += Min;edge[i^1].flow-= Min;cost += edge[i].cost * Min;}flow += Min;}return flow; } char s[111][111]; int r[111],c[111]; int R[111],C[111]; int tot1,tot2; int main() {int n,m;while(~scanf("%d%d",&n,&m)) {if(n == 0 && m == 0) break;init(n*m+2);memset(dis,0,sizeof dis);tot1=tot2=0;int st=n*m+1,ed=n*m+2;int cost;for(int i = 1; i<=n; i++) {scanf("%s",s[i]+1);}for(int i = 1; i<=n; i++) {for(int j = 1; j<=m; j++) {if(s[i][j] == 'm') tot1++,r[tot1] = i,c[tot1] = j;if(s[i][j] == 'H') tot2++,R[tot2] = i,C[tot2] = j;}}for(int i = 1; i<=tot1; i++) {for(int j = 1; j<=tot2; j++) addedge(i,tot1+j,1,abs(c[i]-C[j]) + abs(r[i]-R[j]));}for(int i = 1; i<=tot1; i++) addedge(st,i,1,0);for(int i = 1; i<=tot2; i++) addedge(tot1+i,ed,1,0);int ans = minCostMaxflow(st,ed,cost);printf("%d\n",cost);} return 0 ; }

?

總結

以上是生活随笔為你收集整理的【HDU - 1533】Going Home(网络流,二分图最优匹配,KM算法)的全部內容,希望文章能夠幫你解決所遇到的問題。

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

主站蜘蛛池模板: 蜜桃av噜噜一区二区三区小说 | 久久久久久久999 | 性色在线视频 | 精品一区二区不卡 | 亚洲天堂av一区二区 | 精品久久久久久久免费人妻 | 国产尤物av | 蜜臀久久99精品久久久 | 黄色网免费 | 午夜视频在线观看一区 | 人妻体内射精一区二区三区 | 91国产一区二区 | 夜夜嗨老熟女av一区二区三区 | 在线97视频 | 亚洲777 | 特黄三级又爽又粗又大 | 狠狠爱免费视频 | 国产亚洲精品久久久久动 | www.色午夜.com| 亚洲成av人片在线观看无 | 日韩avwww | 午夜精品一二三区 | 爱情岛论语亚洲入口 | 蜜臀久久99精品久久久久久宅男 | 国产欧美日韩免费 | 在线免费观看黄网站 | 久久国产欧美日韩精品 | 亚洲痴女| 国产乱人| ass亚洲熟妇毛耸耸pics | www.久久av| 夜夜爽天天爽 | 日韩不卡一二区 | www.brazzers.com| 国产 欧美 日韩 一区 | 国产精欧美一区二区三区白种人 | 欧美乱淫视频 | 亚洲福利视频导航 | 青青草视频在线看 | 色人人| 在线免费观看中文字幕 | 国产精品第十页 | 变态另类ts人妖一区二区 | 国产女人18毛片水18精品 | 免费的av| 天天高潮夜夜爽 | 91麻豆精品在线观看 | 手机在线中文字幕 | 亚洲天天干 | 中国黄色1级片 | 色超碰| 99精品在线播放 | 亚洲国产精品久久精品怡红院 | 精品熟女一区二区三区 | 亚洲自拍偷拍网 | 午夜精品久久久久久久久久久 | 国产一区二区三区精品在线 | jizz91| 美女吞精视频 | 在线观看免费福利 | 国产成人在线观看网站 | 成人免费xxxxxx视频 | 国精产品一区二区三区 | 91麻豆网| 亚洲成年人在线 | 国产熟女一区二区三区四区 | 少妇xxxx69| 国产一区二区福利 | 久久538| 人人超碰人人 | juliaann精品艳妇hd | 青娱乐最新视频 | 亚洲福利视频网站 | 草草视频在线播放 | 一个人免费在线观看视频 | 色四虎| 激情黄色小说视频 | 国产又粗又长 | 91传媒理伦片在线观看 | 国产美女被遭强高潮免费网站 | 国产成人啪精品午夜在线观看 | 操模特| 无码人妻精品一区二区 | 亚洲最大的网站 | 99国产精品久久久久 | 欧美性猛交xxxxx水多 | 日韩av在线播放不卡 | 亚洲第一二三区 | 国产在线伊人 | 久久精品一区二区三区黑人印度 | 男性影院 | 中文字幕一区视频 | 日韩av不卡在线观看 | 亚洲欧美日韩专区 | 黄av网| 日日干狠狠干 | 国产网址 | 国产精品久久久久无码av色戒 | 高清无打码 |