1. 程式人生 > >POJ 2195 Going Home(費用流)

POJ 2195 Going Home(費用流)

void 個人 nod const 移動 方向 push class main

http://poj.org/problem?id=2195

題意:

在一個網格地圖上,有n個小人和n棟房子。在每個時間單位內,每個小人可以往水平方向或垂直方向上移動一步,走到相鄰的方格中。對每個小人,每走一步需要支付1美元,直到他走入到一棟房子裏。每棟房子只能容納一個小人。

計算出讓n個小人移動到n個不同的房子需要支付的最小費用。

思路:

源點和每個人相連,容量為1,費用為0。

匯點和每棟房子相連,容量為1,費用為0。

每個人和每棟房子相連,容量為1,費用為人和房子之間的距離。

這樣一來,跑一遍費用流即可。

  1 #include<iostream>
  2
#include<algorithm> 3 #include<cstring> 4 #include<cstdio> 5 #include<sstream> 6 #include<vector> 7 #include<stack> 8 #include<queue> 9 #include<cmath> 10 #include<map> 11 #include<set> 12 using namespace std; 13
typedef long long ll; 14 typedef long long ull; 15 typedef pair<int,int> pll; 16 const int INF = 0x3f3f3f3f; 17 const int maxn = 1000 + 5; 18 19 int n, m, k; 20 21 struct Edge 22 { 23 int from, to, cap, flow, cost; 24 Edge(int u, int v, int c, int f, int w) :from(u), to(v), cap(c), flow(f), cost(w) {}
25 }; 26 27 struct MCMF 28 { 29 int n, m; 30 vector<Edge> edges; 31 vector<int> G[maxn]; 32 int inq[maxn]; 33 int d[maxn]; 34 int p[maxn]; 35 int a[maxn]; 36 37 void init(int n) 38 { 39 this->n = n; 40 for (int i = 0; i<n; i++) G[i].clear(); 41 edges.clear(); 42 } 43 44 void AddEdge(int from, int to, int cap, int cost) 45 { 46 edges.push_back(Edge(from, to, cap, 0, cost)); 47 edges.push_back(Edge(to, from, 0, 0, -cost)); 48 m = edges.size(); 49 G[from].push_back(m - 2); 50 G[to].push_back(m - 1); 51 } 52 53 bool BellmanFord(int s, int t, int &flow, int & cost) 54 { 55 for (int i = 0; i<n; i++) d[i] = INF; 56 memset(inq, 0, sizeof(inq)); 57 d[s] = 0; inq[s] = 1; p[s] = 0; a[s] = INF; 58 59 queue<int> Q; 60 Q.push(s); 61 while (!Q.empty()){ 62 int u = Q.front(); Q.pop(); 63 inq[u] = 0; 64 for (int i = 0; i<G[u].size(); i++){ 65 Edge& e = edges[G[u][i]]; 66 if (e.cap>e.flow && d[e.to]>d[u] + e.cost){ 67 d[e.to] = d[u] + e.cost; 68 p[e.to] = G[u][i]; 69 a[e.to] = min(a[u], e.cap - e.flow); 70 if (!inq[e.to]) { Q.push(e.to); inq[e.to] = 1; } 71 } 72 } 73 } 74 75 if (d[t] == INF) return false; 76 flow += a[t]; 77 cost += d[t] * a[t]; 78 for (int u = t; u != s; u = edges[p[u]].from) 79 { 80 edges[p[u]].flow += a[t]; 81 edges[p[u] ^ 1].flow -= a[t]; 82 } 83 return true; 84 } 85 86 int MincostMaxdflow(int s, int t){ 87 int flow = 0, cost = 0; 88 while (BellmanFord(s, t, flow, cost)); 89 return cost; 90 } 91 }t; 92 93 struct node 94 { 95 int x, y; 96 }people[maxn],house[maxn]; 97 98 int main() 99 { 100 //freopen("in.txt","r",stdin); 101 while(~scanf("%d%d",&n,&m) && n && m) 102 { 103 char c; 104 int cnt_p=0, cnt_h=0; 105 for(int i=0;i<n;i++) 106 { 107 for(int j=0;j<m;j++) 108 { 109 cin>>c; 110 if(c==H) {house[++cnt_h].x=i;house[cnt_h].y=j;} 111 else if(c==m) {people[++cnt_p].x=i;people[cnt_p].y=j;} 112 } 113 } 114 115 int n=cnt_h; 116 int src=0, dst=2*n+1; 117 t.init(dst+1); 118 119 for(int i=1;i<=cnt_p;i++) t.AddEdge(src,i,1,0); 120 for(int i=1;i<=cnt_h;i++) t.AddEdge(n+i,dst,1,0); 121 122 for(int i=1;i<=cnt_p;i++) 123 { 124 for(int j=1;j<=cnt_h;j++) 125 { 126 int dis=abs(people[i].x-house[j].x)+abs(people[i].y-house[j].y); 127 t.AddEdge(i,n+j,1,dis); 128 } 129 } 130 131 printf("%d\n",t.MincostMaxdflow(src,dst)); 132 } 133 return 0; 134 }

POJ 2195 Going Home(費用流)