POJ-2195(最小费用最大流+MCMF算法)
Going Home
POJ-2195
- 这题使用的是最小费用流的模板。
- 建模的时候我的方法出现错误,导致出现WA,根据网上的建图方法没错。
- 这里的建图方法是每次到相邻点的最大容量为INF,而花费为1,因为花费等于距离。但是需要增加一个源点和一个汇点,然后将每个人和源点相连,每个房子和汇点相连,容量都为1,费用都为0.
#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
#include<stack>
#include<cstdio>
using namespace std;
#define INF 0x3f3f3f3f
#define M(a, b) memset(a, b, sizeof(a))
const int N = 1e4 + 5;
const int dx[] = {-1, 1, 0, 0};
const int dy[] = {0, 0, -1, 1};
int n, m;
struct Edge {
int from, to, cap, flow, cost;
};
struct MCMF {
int n, m;
vector<Edge> edges;
vector<int> G[N];
int d[N], inq[N], p[N], a[N];
void init(int n) {
this->n = n;
for (int i = 0; i <= n; ++i) G[i].clear();
edges.clear();
}
void AddEdge(int from, int to, int cap, int cost) {
edges.push_back(Edge{from, to, cap, 0, cost});
edges.push_back(Edge{to, from, 0, 0, -cost});
m = edges.size();
G[from].push_back(m-2); G[to].push_back(m-1);
}
bool spfa(int s, int t, int &flow, int &cost) {
M(inq, 0); M(d, INF);
d[s] = 0; inq[s] = 1; p[s] = 0; a[s] = INF;
queue<int> q;
q.push(s);
while (!q.empty()) {
int x = q.front(); q.pop();
inq[x] = 0;
for (int i = 0; i < G[x].size(); ++i) {
Edge &e = edges[G[x][i]];
if (d[e.to] > d[x] + e.cost && e.cap > e.flow) {
d[e.to] = d[x] + e.cost;
p[e.to] = G[x][i];
a[e.to] = min(a[x], e.cap-e.flow);
if (inq[e.to]) continue;
q.push(e.to); inq[e.to] = 1;
}
}
}
if (d[t] == INF) return false;
flow += a[t];
cost += d[t] * a[t];
int u = t;
while (u != s) {
edges[p[u]].flow += a[t];
edges[p[u]^1].flow -= a[t];
u = edges[p[u]].from;
}
return true;
}
int Mincost(int s, int t) {
int flow = 0, cost = 0;
while (spfa(s, t, flow, cost));
return cost;
}
}solver;
char str[205][205];
bool check(int x, int y) {
if (x>0 && x<=n && y>0 && y<=m) return 1;
return 0;
}
struct node{
int x;
int y;
int num;
};
node men[N];
node hou[N];
char ma[N][N];
int main() {
while (scanf("%d%d", &n, &m), n&&m) {
solver.init(n*m+1);
for (int i = 1; i <= n; ++i) {
scanf("%s", str[i]+1);
for (int j = 1; j <= m; ++j) {
if (str[i][j]=='H') solver.AddEdge((i-1)*m+j, n*m+1, 1, 0);
if (str[i][j]=='m') solver.AddEdge(0, (i-1)*m+j, 1, 0);
for (int k = 0; k < 4; ++k) {
int nx = i+dx[k], ny = j+dy[k];
if (check(nx, ny)) solver.AddEdge((i-1)*m+j, (nx-1)*m+ny, INF, 1);
}
}
}
printf("%d\n", solver.Mincost(0, n*m+1));
//--------------------------------version2
// int n1=201;
// solver.init(2*n1+1);
// int ansh=n,k2=0;
// int ansm=0,k1=0;
// for(int i=0;i<n;i++){
// scanf("%s", ma[i]);
// for(int j=0;j<m;j++){
// if(ma[i][j]=='m'){//人
// ++ansm;
// men[k1].x=i;
// men[k1].y=j;
// men[k1++].num=ansm;
// solver.AddEdge(0,ansm,1,0);
// }else if(ma[i][j]=='H'){
// ++ansh;
// hou[k2].x=i;
// hou[k2].y=j;
// hou[k2++].num=ansh;
// solver.AddEdge(ansh,2*n1+1,1,0);
// }
// }
// }
// for(int i=0;i<k1;i++){
// for(int j=0;j<k2;j++){
// int disc=abs(men[i].x-hou[j].x)+abs(men[i].y-hou[j].y);
// solver.AddEdge(men[i].num,hou[j].num,1,disc);
// }
// }
// printf("%d\n", solver.Mincost(0, n1*2+1));
}
return 0;
}
Either Excellent or Rusty