poj 2195 Going Home(最小费用最大流)

Going Home

Time Limit:1000MSMemory Limit:65536K

Total Submissions:18966Accepted:9675

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.mH.5 5HH..m……………mm..H7 8…H…….H…….H….mmmHmmmm…H…….H…….H….0 0

Sample Output

21028

Source

题意:一个‘H’只能容一个’m’,求每个‘m’都进’H’,‘m’移动的总步数最小是多少。

模板:

#include<cstdio>#include<cstring>#include<algorithm>#include<iostream>#include<vector>#include<cmath>#include<queue>#define INF 1<<29#define N 310111#define ll long longusing namespace std;struct Edge {int to,next,cap,flow,cost;} edge[N];struct node {int x,y;} H[N],M[N];int lh,lm;int head[N],tol;int pre[N],dis[N];bool vis[N];int n,m;int E;int cost;void init(int t) {E=t;tol=0;memset(head,-1,sizeof head);}int dist(node a,node b) {return abs(a.x-b.x)+abs(a.y-b.y);}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 = 0; i < E; 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;}int main() {//freopen("in.txt","r",stdin);while(~scanf("%d%d",&n,&m),n+m) {char c[200];lh=0;lm=0;for(int i=0; i<n; i++) {scanf("%s",c);for(int j=0; j<m; j++) {if(c[j]=='H') {H[lh].x=i;H[lh].y=j;lh++;} else if(c[j]=='m') {M[lm].x=i;M[lm].y=j;lm++;}}}init(lh+lm+2);for(int i=0; i<lh; i++)for(int j=0; j<lm; j++) {addedge(i,lh+j,1,dist(H[i],M[j]));}for(int i=0; i<lh; i++) {addedge(lh+lm,i,1,0);}for(int j=0; j<lm; j++) {addedge(lh+j,lh+lm+1,1,0);}int cost;int res=minCostMaxflow(lm+lh,lm+lh+1,cost);printf("%d\n",cost);}return 0;}

而是面对它们,同它们打交道,

poj 2195 Going Home(最小费用最大流)

相关文章:

你感兴趣的文章:

标签云: