【POJ - 2195】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 0Sample Output
2 10 28題目大意:
? 給你一個N*M的矩陣,' . ' 表示土地,' m '表示一個人,' H '代表一個屋子,現在告訴你屋子數和人數相同,要讓所有人進入屋子內,且一個屋子只能裝一個人,人移動一格花費是1,且只能上下左右移動,那么問你總的最小花費是多少。
解題報告:
人連到起點,房子連到終點,每一個格子可以上下左右四周走,求一個二分圖最優匹配就行了。
AC代碼:
#include<cstdio> #include<iostream> #include<algorithm> #include<queue> #include<map> #include<vector> #include<set> #include<string> #include<cmath> #include<cstring> #define F first #define S second #define ll long long #define pb push_back #define pm make_pair #include <iomanip> using namespace std; const int MAX = 2e5 + 5; const int INF = 0x3f3f3f3f; struct node {int to,c,w,ne; } e[50005<<2]; int n,m,N; int head[MAX],d[MAX],vis[MAX],tot,p[MAX]; void add(int u,int v,int c,int cost=0) {e[++tot].to = v;e[tot].c = c;e[tot].w = cost;e[tot].ne = head[u];head[u] = tot;e[++tot].to = u;e[tot].c = 0; e[tot].w = -cost;e[tot].ne = head[v];head[v] = tot; } bool bfs(int s,int t) {for(int i = 0; i<=N; i++) d[i]=INF,vis[i]=0;d[s]=0;queue<int>q;q.push(s);while(!q.empty()) {int u=q.front();q.pop();vis[u]=0;for(int i=head[u]; ~i; i=e[i].ne) {int j=e[i].to;if(e[i].c&&d[j]>d[u]+e[i].w) {d[j]=d[u]+e[i].w;p[j]=i;if(!vis[j])vis[j]=1,q.push(j);}}}return d[t]<INF; } int MCMF(int s,int t,int &flow) {ll ans=0;while(bfs(s,t)) {int x=t,f=INF;while(x!=s) {f = min(f,e[p[x]].c),x=e[p[x]^1].to;}flow += f;ans+=1LL*d[t]*f;x=t;while(x!=s) {e[p[x]].c-=f,e[p[x]^1].c+=f;x=e[p[x]^1].to;}}return ans; } void init(int NN) {tot=1;for(int i = 0; i<=NN; i++) head[i] = -1; } char s[105][105]; int id(int i,int j) {return (i-1)*m+j; } int nx[4] = {0,1,0,-1}; int ny[4] = {1,0,-1,0}; int main() {while(~scanf("%d%d",&n,&m)) {if(n+m==0) break;int st=0,ed=n*m+1;N=n*m+1; init(n*m+1);for(int i = 1; i<=n; i++) {scanf("%s",s[i]+1);for(int j = 1; j<=m; j++) {if(s[i][j] == 'm') add(st,id(i,j),1,0);if(s[i][j] == 'H') add(id(i,j),ed,1,0); }}for(int i = 1; i<=n; i++) {for(int j = 1; j<=m; j++) {for(int k = 0; k<4; k++) {int tx = i+nx[k];int ty = j+ny[k];if(tx<1||tx>n||ty<1||ty>m) continue;add(id(i,j),id(tx,ty),INF,1);}}}int ansf=0;int ansc=MCMF(st,ed,ansf);printf("%d\n",ansc); }return 0 ; }?
總結
以上是生活随笔為你收集整理的【POJ - 2195】Going Home(二分图最优匹配,费用流 或 KM)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: AMD Zen4架构APU集显性能大爆发
- 下一篇: 【HDU - 3746 】Cyclic