POJ2195--Going Home
来源:互联网 发布:天正软件提示过期 编辑:程序博客网 时间:2024/06/16 07:43
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.
![](http://poj.org/images/2195_1.jpg)
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.
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.
![](http://poj.org/images/2195_1.jpg)
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.mH.5 5HH..m...............mm..H7 8...H.......H.......H....mmmHmmmm...H.......H.......H....0 0
Sample Output
21028
#include <iostream>#include <cstdio>#include <cstring>#include <cmath>using namespace std;#define maxn 10080#define edge 1000008#define inf 0x3f3f3f3fint first[maxn];int vv[edge],ww[edge],nxt[edge],cst[edge];int e;int pre[maxn],pos[maxn],dis[maxn],que[maxn*10];bool vis[maxn];inline int min(int a,int b){return a>b?b:a;}void addEdge(int u,int v,int w,int cost){vv[e] = v;ww[e] = w;cst[e] = cost;nxt[e] = first[u];first[u] = e++;vv[e] = u;ww[e] = 0;cst[e] = -cost;nxt[e] = first[v];first[v] = e++;}bool spfa(int s,int t){int i;memset(pre,-1,sizeof(pre));memset(vis,0,sizeof(vis));int head,tail;head = tail = 0;for(i=0;i<maxn;i++)dis[i] = inf;que[tail++] = s;pre[s] = s;dis[s] = 0;vis[s] = 1;while(head != tail){int u = que[head++];vis[u] = 0;for(i=first[u];i!=-1;i=nxt[i]){int v = vv[i];if(ww[i] > 0 && dis[u] + cst[i] < dis[v]){dis[v] = dis[u] + cst[i];pre[v] = u;pos[v] = i;if(!vis[v]){vis[v] = 1;que[tail++] = v;}}}}return pre[t]!=-1;}int MinCostFlow(int s,int t){int i;int cost = 0;while(spfa(s,t)){int f = inf;for(i=t;i!=s;i=pre[i]){if(ww[pos[i]] < f) f = ww[pos[i]];}cost += dis[t] * f;for(i=t;i!=s;i=pre[i]){ww[pos[i]] -= f;ww[pos[i]^1] += f;}}return cost;}struct PP{int r,c;}pp[10008];struct HS{int r,c;}hs[108];int main(){//freopen("in.txt","r",stdin);int n,m;while(scanf("%d%d",&n,&m)==2 && (n||m)){memset(first,-1,sizeof(first));e = 0;getchar();int h,p;h = p = 0;for(int i=1;i<=n;i++){for(int j=1;j<=m;j++){char c = getchar();if(c == 'H'){hs[h].r = i;hs[h++].c = j;}else if(c == 'm'){pp[p].r = i;pp[p++].c = j;}}getchar();}for(int i=0;i<p;i++){for(int j=0;j<h;j++){int cost = abs(pp[i].r-hs[j].r)+abs(pp[i].c-hs[j].c);addEdge(i+1,j+p+1,1,cost);}}for(int i=0;i<p;i++){addEdge(0,i+1,1,0);}for(int i=0;i<h;i++){addEdge(i+p+1,h+p+1,1,0);}int ans = MinCostFlow(0,h+p+1);printf("%d\n",ans);}return 0;}
- poj2195 Going Home
- poj2195 Going Home
- poj2195 - Going Home
- POJ2195--Going Home
- poj2195 Going Home
- poj2195 Going Home
- poj2195 Going Home
- POJ2195 Going Home
- POJ2195 Going Home
- 【算法】POJ2195 Going Home
- 【poj2195】Going Home
- POJ2195: Going Home 题解
- poj2195 Going Home
- poj2195——Going Home
- poj2195——Going Home
- poj2195 Going Home 费用流
- POJ2195——Going Home
- 费用流 poj2195 Going Home
- Spring 整合 Apache CXF发布webService
- Linux sqlite3基本命令
- Merge Two Sorted Lists
- intent.setFlags方法中的参数值含义
- ob_start()超全用法
- POJ2195--Going Home
- android 读取短信
- WebBrowser控件——设置滚动条位置
- hdu2080夹角有多大II
- mysql 基准测试和性能分析
- Py2exe打包成exe
- RAC关键信息(OCR/VOTE DISK/ASM DISK HEADER)备份
- uva11181 - Probability|Given(条件概率)
- TCP 與 UDP