HDU 1533Going Home(费用流)

来源:互联网 发布:算法基础 第5版 下载 编辑:程序博客网 时间:2024/05/05 18:03
Problem 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


设一个汇点,一个源点

从源点到所有m连一条流量为1费用为0的边。从所有m到H连一条流量为1费用为距离的边。从所有H连一条流量为1费用为0的边到汇点。

然后套个网络流模板。这样连得原因就是保证一一对应。不信你画个图自己看看。


AC代码:

# include <stdio.h># include <string.h># include <algorithm># include <math.h># include <stdlib.h># include <vector>using namespace std;typedef long long int ll;struct edge{int to, cap, cost, rev;};int V;//为节点总数const int maxn=10010;const int inf=2000000000; vector<int> H;//H的标号vector<int> M;//M的标号int n, m;//n为行,m为列char g[110][110];vector <edge> G[maxn];int dist[maxn];int preve[maxn], prevv[maxn];void add_edge(int from, int to, int cap, int cost){//加边 G[from].push_back( (edge) { to, cap, cost, G[to].size()});G[to].push_back((edge){from, 0, -cost, G[from].size()-1});}void init(){//初始化 H.clear();M.clear();for(int i=0; i<10009; i++){G[i].clear(); }    memset(preve, 0, sizeof(preve));    memset(prevv, 0, sizeof(prevv));    memset(dist, 0, sizeof(dist));}int min_cost_flow(int s, int t, int f){//费用流算法 int res=0;while(f>0){fill(dist, dist+V, inf);dist[s]=0;bool update=true;while(update){update=false;for(int v=0; v<V; v++){if(dist[v]==inf) continue;for(int i=0; i<G[v].size(); i++){edge &e=G[v][i];if(e.cap>0&&dist[e.to]>dist[v]+e.cost){dist[e.to]=dist[v]+e.cost;prevv[e.to]=v;preve[e.to]=i;update=true;}}}}if(dist[t]==inf){return -1;}int d=f;for(int v=t; v!=s; v=prevv[v]){d=min(d, G[prevv[v]][preve[v]].cap);}f-=d;res+=d*dist[t];for(int v=t; v!=s; v=prevv[v]){edge &e=G[prevv[v]][preve[v]];e.cap-=d;G[v][e.rev].cap+=d; }}return res;}int main(){int i, j, k; while(1){scanf("%d%d", &n, &m);getchar();if(m==0&&n==0){break;}for(i=1; i<=n; i++){scanf("%s", g[i]+1);}init();for(i=1; i<=n; i++){for(j=1; j<=m; j++){if(g[i][j]=='H'){H.push_back((i-1)*m+j);}if(g[i][j]=='m'){M.push_back((i-1)*m+j);}}}//构图int Size=H.size();    V=Size*2+2; //定点总数,0为原点,v-1为汇点     //s到H构图         for(i=0; i<Size; i++){        add_edge(0, i+1, 1, 0);}//H到M for(i=0; i<Size; i++){ int x1, y1;    if(H[i]%m==0){    x1=H[i]/m;y1=m;}else{x1=H[i]/m+1;y1=H[i]%m;}for(j=0; j<Size; j++){ int x2, y2; if(M[j]%m==0){    x2=M[j]/m;y2=m;}else{x2=M[j]/m+1;y2=M[j]%m;}add_edge(i+1, Size+j+1, 1, abs(x1-x2)+abs(y1-y2));} } //M到t  for(i=0; i<Size; i++){ add_edge(Size+1+i, 2*Size+1, 1, 0); } printf("%d\n", min_cost_flow(0, 2*Size+1, Size));}return 0;}



0 0
原创粉丝点击