Hmm ~ The first write cost flow question...
This is the template Question of the billing flow. No more bare questions can be found.
Graph creation: Each m (man) serves as the source point, each H (house) serves as the sink point, and each source point and sink point connects one edge separately, the traffic on this edge is 1 (because each source point can only take one edge to the sink point), and the cost is the number of steps from the source point to the sink point, because there are multiple source points and sink points, to create a super source point and a super sink point, the traffic between the super source point and each source point is 1, and the cost is 0 (to avoid extra costs ).
Follow this picture to run a billing flow.
Hanging up the code, using the forward to the star
1 # include <cstdio> 2 # include <cstring> 3 # include <cmath> 4 # include <iostream> 5 # include <algorithm> 6 # include <set> 7 # include <map> 8 # include <stack> 9 # include <vector> 10 # include <queue> 11 # include <string> 12 # include <sstream> 13 # define maxn 3000 14 # define maxm 50000 15 # define Inf (1 <30) 16 # define EPS 0.000001 17 # define all (x) X. begin (), X. end () 18 # define ins (x) inserter (X, X. begin () 19 USI Ng namespace STD; 20 int I, j, k, n, m, X, Y, T, num, W, man, house, Hou [305] [2], MAN [305] [2]; 21 int head [maxn], vis [maxn], DIS [maxn], POS [maxn], edge, size; 22 char s [305] [305]; 23 struct edgenode 24 {25 int to, next, W, cost; 26} edge [maxm]; 27 28 void add_edge (int x, int y, int W, int cost) 29 {30 edge [edge]. to = y; 31 edge [edge]. W = W; 32 edge [edge]. cost = cost; 33 edge [edge]. next = head [X]; 34 head [x] = edge; 35 edge ++; 3 6 37 38 edge [edge]. to = x; 39 edge [edge]. W = 0; 40 edge [edge]. cost =-cost; 41 edge [edge]. next = head [y]; 42 head [y] = edge; 43 edge ++; 44} 45 46 bool spfa (int s, int t) 47 {48 int U, v, I; 49 queue <int> q; 50 memset (VIS, 0, sizeof (VIS); 51 for (I = 0; I <size; I ++) dis [I] = inf; 52 dis [s] = 0; 53 vis [s] = 1; 54 Q. push (s); 55 while (! Q. empty () 56 {57 U = Q. front (); q. pop (); vis [u] = 0; 58 for (I = head [u]; I! =-1; I = edge [I]. next) 59 {60 V = edge [I]. to; 61 If (edge [I]. w> 0 & dis [u] + edge [I]. cost <dis [v]) 62 {63 dis [v] = dis [u] + edge [I]. cost; 64 POS [v] = I; 65 if (! Vis [v]) 66 {67 vis [v] = 1; 68 Q. Push (V); 69} 70} 71} 72} 73 return dis [T]! = Inf; 74} 75 int mincostflow (int s, int t) 76 {77 int I, cost = 0, flow = 0; 78 while (spfa (S, T )) 79 {80 int d = inf; 81 for (I = T; I! = S; I = edge [POS [I] ^ 1]. to) 82 {83 d = min (D, edge [POS [I]. w); 84} 85 for (I = T; I! = S; I = edge [POS [I] ^ 1]. to) 86 {87 edge [POS [I]. w-= D; 88 edge [POS [I] ^ 1]. W + = D; 89} 90 flow + = D; 91 cost + = dis [T] * D; 92} 93 return cost; // flow is the maximum stream value 94} 95 96 int main () 97 {98 while (scanf ("% d", & N, & M), N + M) 99 {100 memset (Head,-1, sizeof (head); 101 edge = man = house = num = 0; 102 for (I = 0; I <N; I ++) scanf ("% s", s [I]); 103 for (I = 0; I <n; I ++) 104 {105 for (j = 0; j <m; j ++) 106 {107 If (s [I] [J] = 'M ') 108 {109 man ++; 110 MAN [Man] [0] = I; 111 MAN [Man] [1] = J; 112} else113 if (s [I] [J] = 'H') 114 {115 house ++; 116 Hou [House] [0] = I; 117 Hou [House] [1] = J; 118} 119} 120} 121 size = man + house + 2; 122/* Super source point 0, to the sides of each individual */123 for (I = 1; I <= man; I ++) 124 {125 add_edge (0, I ); 126} 127/* edge between each source point and each sink */128 for (I = 1; I <= man; I ++) 129 {130 for (j = 1; j <= house; j ++) 131 {132 add_edge (I, man + J, 1, ABS (MAN [I] [0]-hou [J] [0]) + ABS (MAN [I] [1]-hou [J] [1]); 133} 134} 135/* Super sink point 0, to individual sides */136 for (I = 1; I <= House; I ++) 137 {138 add_edge (MAN + I, man + house +, 0); 139} 140 printf ("% d \ n", mincostflow (0, man + house + 1); 141} 142 return 0; 143}
View code
It is said that the KM algorithm can be used to write the code of the KM algorithm...
[Network stream #2] maximum streaming template for the minimum cost of HDU 1533