PKU 2195 - Going Home (最小费用最大流)
题意
一些人要到各自的屋子去,每一步都有权值,求最小的权值。
思路
最小费用最大流模板题,等周末学习了KM算法再来一遍。
代码
#include <cstdio>
#include <stack>
#include <set>
#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <functional>
#include <cstring>
#include <algorithm>
#include <cctype>
#include <string>
#include <map>
#include <cmath>
#define LL long long
#define SZ(x) (int)x.size()
#define Lowbit(x) ((x) & (-x))
#define MP(a, b) make_pair(a, b)
#define MS(arr, num) memset(arr, num, sizeof(arr))
#define PB push_back
#define F first
#define S second
#define ROP freopen("input.txt", "r", stdin);
#define MID(a, b) (a + ((b - a) >> 1))
#define LC rt << 1, l, mid
#define RC rt << 1|1, mid + 1, r
#define LRT rt << 1
#define RRT rt << 1|1
#define BitCount(x) __builtin_popcount(x)
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
using namespace std;
const int MAXN = 200 + 10;
const int MOD = 1e9 + 7;
const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
typedef pair<int, int> pii;
typedef vector<int>::iterator viti;
typedef vector<pii>::iterator vitii;
char mp[MAXN][MAXN];
struct EDGE
{
int from, to, cap, flow, cost;
};
struct MCMF
{
int m, st, ed;
vector<EDGE> edges;
vector<int> G[MAXN];
int inq[MAXN]; //是否在队列中
int d[MAXN]; //bellman-Ford
int p[MAXN]; //上一条弧
int a[MAXN]; //可改进量
void init(int n)
{
this->ed = n;
for (int i = 0; i <= n; i++) G[i].clear();
edges.clear();
}
void add_edge(int from, int to, int cap, int cost)
{
edges.PB((EDGE){from, to, cap, 0, cost});
edges.PB((EDGE){to, from, 0, 0, -cost});
m = SZ(edges);
G[from].PB(m - 2);
G[to].PB(m - 1);
}
bool SPFA(int st, int ed, int &flow, int &cost)
{
fill(d, d + ed + 1, INF);
MS(inq, 0);
d[st] = 0; inq[st] = 1; p[st] = -1; a[st] = INF;
queue<int> qu;
qu.push(st);
while (!qu.empty())
{
int u = qu.front(); qu.pop();
inq[u] = 0;
for (int i = 0; i < SZ(G[u]); i++)
{
EDGE &e = edges[G[u][i]];
if (e.cap > e.flow && d[e.to] > d[u] + e.cost)
{
d[e.to] = d[u] + e.cost;
p[e.to] = G[u][i];
a[e.to] = min(a[u], e.cap - e.flow);
if (!inq[e.to])
qu.push(e.to), inq[e.to] = 1;
}
}
}
if (d[ed] == INF) return false;
flow += a[ed]; cost += d[ed] * a[ed];
int u = ed;
while (u != st)
{
edges[p[u]].flow += a[ed];
edges[p[u] ^ 1].flow -= a[ed];
u = edges[p[u]].from;
}
return true;
}
}MFMC;
vector<pii> hos, man;
int main()
{
//ROP;
int row, col, i, j;
while (scanf("%d%d", &row, &col), row + col)
{
hos.clear(); man.clear();
for (i = 0; i < row; i++) scanf("%s", mp[i]);
for (i = 0; i < row; i++)
for (j = 0; j < col; j++)
if (mp[i][j] == 'H') hos.PB(MP(i, j));
else if (mp[i][j] == 'm') man.PB(MP(i, j));
int ed = SZ(man) + SZ(hos) + 1, st = 0;
MFMC.init(ed);
for (i = 0; i < SZ(man); i++)
{
MFMC.add_edge(st, i + 1, 1, 0);
for (j = 0; j < SZ(hos); j++)
{
int cost = abs(man[i].F - hos[j].F) + abs(man[i].S - hos[j].S);
MFMC.add_edge(i + 1, SZ(man) + j + 1, 1, cost);
}
}
for (i = 0; i < SZ(hos); i++) MFMC.add_edge(SZ(man) + i + 1, ed, 1, 0);
int cost = 0, flow = 0;
while (MFMC.SPFA(st, ed, flow, cost));
printf("%d\n", cost);
}
return 0;
}