hdu 1533 Going Home (最小费用最大流)

Posted 抓不住Jerry的Tom

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hdu 1533 Going Home (最小费用最大流)相关的知识,希望对你有一定的参考价值。

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

 

有几个人要回几个家,每人每走一步花费1块钱,问大家一共最少花费多少钱?

最小费用最大流模板题 献祭上模板

 

费用流模板

  1 #include <bits/stdc++.h>
  2 
  3 using namespace std;
  4 const int maxn = 200;
  5 const int N = 500020;
  6 const int M = 500020;
  7 const int inf = 0x3f3f3f3f;
  8 
  9 int n,m;
 10 struct Edge {
 11   int from, to, cap, flow;
 12   int  cost;
 13 };
 14 inline int Min(int aa,int bb)
 15 {
 16     return aa<bb?aa:bb;
 17 }
 18 vector < pair<int,int> > men;
 19 vector < pair<int,int> > house;
 20 struct MCMF {
 21     int n, m, s, t;
 22     vector<Edge> edges;
 23     vector<int> G[N];
 24     int inq[N];         // 是否在队列中
 25     int d[N];           // Bellman-Ford
 26     int p[N];           // 上一条弧
 27     int a[N];           // 可改进量
 28     void init(int n) {
 29         this->n = n;
 30         for(int i = 0; i < n; i++) G[i].clear();
 31         edges.clear();
 32     }
 33 
 34     void addedge(int from, int to, int cap, int cost) {
 35         edges.push_back((Edge){from, to, cap, 0, cost});
 36         edges.push_back((Edge){to, from, 0, 0, -cost});
 37         m = edges.size();
 38         G[from].push_back(m-2);
 39         G[to].push_back(m-1);
 40     }
 41     bool BellmanFord(int s, int t, int& flow,int& cost) {
 42         for(int i = 0; i < n; i++) d[i] = inf;
 43         memset(inq, 0, sizeof(inq));
 44         d[s] = 0; inq[s] = 1; p[s] = 0; a[s] = inf;
 45         queue<int> Q;
 46         Q.push(s);
 47         while(!Q.empty()) {
 48           int u = Q.front(); Q.pop();
 49           inq[u] = 0;
 50           int l=G[u].size();
 51           for(int i = 0; i < l; i++) {
 52             Edge& e = edges[G[u][i]];
 53             if(e.cap > e.flow && d[e.to] > d[u] + e.cost) {
 54               d[e.to] = d[u] + e.cost;
 55               p[e.to] = G[u][i];
 56               a[e.to] = Min(a[u], e.cap - e.flow);
 57               if(!inq[e.to]) { Q.push(e.to); inq[e.to] = 1; }
 58             }
 59           }
 60         }
 61         if(d[t] == inf) return false;
 62         cost += d[t]*a[t];
 63         int u = t;
 64         while(u != s) {
 65           edges[p[u]].flow += a[t];
 66           edges[p[u]^1].flow -= a[t];
 67           u = edges[p[u]].from;
 68         }
 69         return true;
 70     }
 71     int Mincost(int s, int t) {
 72         int cost = 0;
 73         int flow=0;
 74         while(BellmanFord(s, t,flow, cost));
 75         return cost;
 76     }
 77 }g;
 78 char s[maxn][maxn];
 79 int getid (int x,int y)
 80 {
 81     return (x-1)*m+y;
 82 }
 83 int x[maxn],xx[maxn];
 84 int y[maxn],yy[maxn];
 85 int nx,ny;
 86 int main() {
 87     //freopen("de.txt","r",stdin);
 88     while(~scanf("%d%d", &n, &m)) {
 89         if (n==0&&m==0)
 90             break;
 91         nx = ny = 0;
 92         g.init(n*m+100);
 93         int ffrom = n*m+ 1;int tto = n*m + 2;
 94         for(int i = 1; i <= n; ++i) {
 95             scanf("%s", s[i] + 1);
 96             for(int j = 1; j <= m; ++j) {
 97                 if(s[i][j] == m) {
 98                     nx++;
 99                     x[nx] = i;
100                     y[nx] = j;
101                 }
102                 if(s[i][j] == H) {
103                     ny++;
104                     xx[ny] = i;
105                     yy[ny] = j;
106                 }
107             }
108         }
109 
110         for(int i = 1; i <= nx; ++i) {
111             for(int j = 1; j <= ny; ++j) {
112                 int d = abs(x[i] - xx[j]);
113                 d += abs(y[i] - yy[j]);
114                 g.addedge(getid(x[i],y[i]), getid(xx[j],yy[j]), 1, d);
115             }
116         }
117         for(int i = 1; i <= nx; ++i) g.addedge(ffrom, getid(x[i],y[i]), 1, 0);
118         for(int i = 1; i <= ny; ++i) g.addedge(getid(xx[i],yy[i]), tto, 1, 0);
119         printf("%d\n", g.Mincost(ffrom, tto));
120     }
121     return 0;
122 }

 

 

zkw费用流,每次增广多条路

  1 #include <bits/stdc++.h>
  2 
  3 using namespace std;
  4 const int maxn = 200;
  5 const int N = 500020;
  6 const int M = 500020;
  7 const int inf = 0x3f3f3f3f;
  8 
  9 int n,m;
 10 struct Edge {
 11   int from, to, cap, flow;
 12   int  cost;
 13 };
 14 inline int Min(int aa,int bb)
 15 {
 16     return aa<bb?aa:bb;
 17 }
 18 
 19 char s[maxn][maxn];
 20 
 21 struct zkw {
 22     int C, D;
 23     int s, t;
 24     int fst[N], nxt[M], vv[M], cap[M], flow[M], cost[M], e;
 25     bool vis[N];
 26     void init() {
 27         memset(fst, -1, sizeof fst);
 28         e = 0;
 29         C = D = 0;
 30     }
 31     void add(int u, int v, int f, int c) {
 32         vv[e] = v, nxt[e] = fst[u], cost[e] = c, cap[e] = f, flow[e] = 0, fst[u] = e++;
 33         vv[e] = u, nxt[e] = fst[v], cost[e] = -c, cap[e] = flow[e] = 0, fst[v] = e++;
 34     }
 35     int aug(int u, int f) {
 36         if(u == t) {
 37             C += D * f;
 38             return f;
 39         }
 40         vis[u] = 1;
 41         int tmp = f;
 42         for(int i = fst[u]; ~i; i = nxt[i]) {
 43             int v = vv[i];
 44             if(cap[i] > flow[i] && cost[i] == 0 && !vis[v]) {
 45                 int d = aug(v, tmp < cap[i] - flow[i]? tmp: cap[i] - flow[i]);
 46                 flow[i] += d;
 47                 flow[i^1] -= d;
 48                 tmp -= d;
 49                 if(!tmp) return f;
 50             }
 51         }
 52         return f - tmp;
 53     }
 54     bool modLabel() {
 55         int d = inf;
 56         for(int i = 0; i <= t; ++i) if(vis[i]) {
 57             for(int j = fst[i]; ~j; j = nxt[j]) {
 58                 int v = vv[j];
 59                 if(cap[j] > flow[j] && !vis[v] && cost[j] < d) d = cost[j];
 60             }
 61         }
 62         if(d == inf) return 0;
 63         for(int i = 0; i <= t; ++i) {
 64             if(vis[i]) {
 65                 for(int j = fst[i]; ~j; j = nxt[j]) {
 66                     int v = vv[j];
 67                     cost[j] -= d;
 68                     cost[j^1] += d;
 69                 }
 70             }
 71         }
 72         D += d;
 73         return 1;
 74     }
 75     int gao(int s, int t) {
 76         this -> s = s, this -> t = t;
 77         do do memset(vis, 0, sizeof vis);
 78         while(aug(s, inf)); while(modLabel());
 79         return C;
 80     }
 81 }g;
 82 
 83 int getid (int x,int y)
 84 {
 85     return (x-1)*m+y;
 86 }
 87 int x[maxn],xx[maxn];
 88 int y[maxn],yy[maxn];
 89 int nx,ny;
 90 int main() {
 91     //freopen("de.txt","r",stdin);
 92     while(~scanf("%d%d", &n, &m)) {
 93         if (n==0&&m==0)
 94             break;
 95         nx = ny = 0;
 96         g.init();
 97         int ffrom = n*m+ 1;int tto = n*m + 2;
 98         for(int i = 1; i <= n; ++i) {
 99             scanf("%s", s[i] + 1);
100             for(int j = 1; j <= m; ++j) {
101                 if(s[i][j] == m) {
102                     nx++;
103                     x[nx] = i;
104                     y[nx] = j;
105                 }
106                 if(s[i][j] == H) {
107                     ny++;
108                     xx[ny] = i;
109                     yy[ny] = j;
110                 }
111             }
112         }
113 
114         for(int i = 1; i <= nx; ++i) {
115             for(int j = 1; j <= ny; ++j) {
116                 int d = abs(x[i] - xx[j]);
117                 d += abs(y[i] - yy[j]);
118                 g.add(getid(x[i],y[i]), getid(xx[j],yy[j]), 1, d);
119             }
120         }
121         for(int i = 1; i <= nx; ++i) g.add(ffrom, getid(x[i],y[i]), 1, 0);
122         for(int i = 1; i <= ny; ++i) g.add(getid(xx[i],yy[i]), tto, 1, 0);
123         printf("%d\n", g.gao(ffrom, tto));
124     }
125     return 0;
126 }

 





以上是关于hdu 1533 Going Home (最小费用最大流)的主要内容,如果未能解决你的问题,请参考以下文章

HDU 1533 Going Home(最小费用流)

hdu 1533 Going Home 最小费用最大流 入门题

POJ 2195 & HDU 1533 Going Home(最小费用最大流)

HDOJ:1533-Going Home(最小费用流)

Going Home HDU - 1533 (费用流)

hdoj 1533 Going Home 最小费用最大流KM入门题