题意:
给你一个N行M列的矩阵,其中“.”代表空地,“H”代表房子,“m”代表人,其中有n个房子和n个人。现在要求每个人进入一间房子,且人走一步需要支付1美元。
求最小需要花费多少美元才能让所有人都进入到房子中(每个人只能进入一间房子,每个房子只能容纳一个人)。
链接 :hdu 1533MCMF建图 源点到人(sp, i, 1, 0)
房子到汇点(j + n, tp, 1, 0)
每个人到每个房子(i, j + n, 1, dis)
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <sstream>
#define MAX_V 1005
#define INF 0x3f3f3f3f
using namespace std;
//最小费用最大流模版.求最大费用最大流建图时把费用取负即可。
//无向边转换成有向边时需要拆分成两条有向边。即两次加边。
const int maxn = 1010;
const int maxm = 1000200;
const int inf = 0x3f3f3f3f;
struct Edge {
int v, cap, cost, next;
}p[maxm << 1];
int e, sumFlow, n, m, st, en;
int head[maxn], dis[maxn], pre[maxn];
bool vis[maxn];
void init() {
e = 0;
memset(head, -1, sizeof(head));
}
void addEdge(int u, int v, int cap, int cost) {
p[e].v = v; p[e].cap = cap; p[e].cost = cost;
p[e].next = head[u]; head[u] = e++;
p[e].v = u; p[e].cap = 0; p[e].cost = -cost;
p[e].next = head[v]; head[v] = e++;
}
bool spfa(int s,int t, int n) {
int u, v;
queue<int>q;
memset(vis, false, sizeof(vis));
memset(pre, -1, sizeof(pre));
for(int i = 0; i <= n; i++)
dis[i] = inf;
vis[s] = true;
dis[s] = 0;
q.push(s);
while(!q.empty()) {
u = q.front();
q.pop();
vis[u] = false;
for(int i = head[u]; i != -1; i = p[i].next) {
v = p[i].v;
if(p[i].cap && dis[v] > dis[u] + p[i].cost) {
dis[v] = dis[u] + p[i].cost;
pre[v] = i;
if(!vis[v]) {
q.push(v);
vis[v] = true;
}
}
}
}
if(dis[t] == inf)
return false;
return true;
}
int MCMF(int s, int t, int n) {
int flow = 0; // 总流量
int minflow, mincost;
mincost = 0;
while(spfa(s, t, n)) {
minflow = inf + 1;
for(int i = pre[t]; i != -1; i = pre[p[i^1].v]) {
if(p[i].cap < minflow) {
minflow = p[i].cap;
}
}
flow += minflow;
for(int i = pre[t]; i != -1; i = pre[p[i^1].v]) {
p[i].cap -= minflow;
p[i^1].cap += minflow;
}
mincost += dis[t] * minflow;
}
sumFlow = flow; // 最大流
return mincost;
}
int x1[105], x2[105], yy[105], y2[105];
int main ()
{
int t, kcase = 0;
int N, M;
while(~scanf("%d %d", &N, &M) && N && M) {
int k1 = 0, k2 = 0;
char str[105];
for(int i = 0; i < N; i++) {
scanf("%s", str);
for(int j = 0; j < M; j++) {
if(str[j] == 'm') {
x1[++k1] = i;
yy[k1] = j;
}
if(str[j] == 'H') {
x2[++k2] = i;
y2[k2] = j;
}
}
}
init();
n = k1;
//cout << k1;
for(int i = 1; i <= n; i++) {
addEdge(0, i, 1, 0);
addEdge(n + i, n * 2 + 1, 1, 0);
for(int j = 1; j <= n; j++) {
int k = (abs(x1[i] - x2[j]) + abs(yy[i] - y2[j]));
//cout << " " << w[i][j] ;
addEdge(i, j + n, 1, k);
}
//cout <<endl;
}
int ans = MCMF(0, 2 * n + 1, 2 * n + 1);
printf("%d\n", ans);
}
return 0;
}
w[i][j] = - dis......
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <stack>
#include <queue>
#define INF 0x3f3f3f3f
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 1005;
int n, nx, ny, w[maxn][maxn];
int link[maxn], lx[maxn], ly[maxn], slack[maxn];
bool visx[maxn], visy[maxn];
char mp[maxn][maxn];
int x1[maxn], x2[maxn];
int yy[maxn], y2[maxn];
bool dfs(int x){
visx[x] = true;
for(int y = 1; y <= ny; y++){
if(visy[y]) continue;
int t = lx[x] + ly[y] - w[x][y];
if(t == 0){
visy[y] = true;
if(link[y] == -1 || dfs(link[y])){
link[y] = x;
return true;
}
}
else
if(slack[y] > t){ //不在相等子图中slack取最小的
slack[y] = t;
}
}
return false;
}
int KM(){
nx = ny = n;
memset(link, -1, sizeof(link));
memset(ly, 0, sizeof(ly));
for(int i = 1; i <= nx; i++){ //lx 初始化为与它关联边中最大的
lx[i] = -INF;
for(int j = 1; j <= ny; j++){
if(w[i][j] > lx[i]) lx[i] = w[i][j];
}
}
for(int x = 1; x <= nx; x++){
for(int i = 1; i <= ny; i++){
slack[i] = INF;
}
while(true){
memset(visx, false, sizeof(visx));
memset(visy, false, sizeof(visy));
//若成功(找到了增广路),则该点增广完毕,下一个点进入增广
if(dfs(x)) break;
//若失败,则需要改变一些点的顶标,使得图中可行边的数量增加
//(1)将所有在增广轨中的 X 方点的标号 全部减去一个常数 d ;
//(2)将所有在增广轨中的 Y 方点的标号 全部加上一个常数 d ;
int d = INF;
for(int i = 1; i <= ny; i++){
if(!visy[i] && d > slack[i]) d = slack[i];
}
for(int i = 1; i <= nx; i++){
if(visx[i]) lx[i] -= d;
}
for(int i = 1; i <= ny; i++){
if(visy[i]) ly[i] += d;
else slack[i] -= d;
}
}
}
int res = 0;
for(int i = 1; i <= ny; i++){
if(link[i] > -1) res += w[link[i]][i];
}
return res;
}
int main ()
{
int N, M;
while(~scanf("%d %d", &N, &M) && N && M) {
int k1 = 0, k2 = 0;
for(int i = 0; i < N; i++) {
scanf("%s", mp[i]);
for(int j = 0; j < M; j++) {
if(mp[i][j] == 'm') {
x1[++k1] = i;
yy[k1] = j;
}
if(mp[i][j] == 'H') {
x2[++k2] = i;
y2[k2] = j;
}
}
}
n = k1;
//cout << k1;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
w[i][j] = -(abs(x1[i] - x2[j]) + abs(yy[i] - y2[j]));
//cout << " " << w[i][j] ;
}
//cout <<endl;
}
int ans = -KM();
printf("%d\n", ans);
}
return 0;
}