POJ - 2195 Going Home 最小费用最大流
题意:多组输入,n行m列矩阵包含相等个数的 ‘m’ 和 ‘ H ’ 每个men要到达Home,每移动一个格子耗费 1,求最小花费。题解:很明显每个人都要到达且移动次数最少,即最小花费最大流。源点->men->Home->汇点建图#include <iostream>#include <stdio.h>#include <que...
·
题意:多组输入,n行m列矩阵包含相等个数的 ‘m’ 和 ‘ H ’ 每个men要到达Home,每移动一个格子耗费 1,求最小花费。
题解:很明显每个人都要到达且移动次数最少,即最小花费最大流。
源点->men->Home->汇点建图
#include <iostream>
#include <stdio.h>
#include <queue>
#include <cstring>
#include <algorithm>
#define il inline
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 1e4;
const int INF = 0x7fffffff;
struct edge {
int to, capacity, cost, rev;
edge() {}
edge(int to, int _capacity, int _cost, int _rev) :to(to), capacity(_capacity), cost(_cost), rev(_rev) {}
};
struct Min_Cost_Max_Flow {
int V, H[maxn + 5], dis[maxn + 5], PreV[maxn + 5], PreE[maxn + 5];
vector<edge> G[maxn + 5];
//调用前初始化
void Init(int n) {
V = n;
for (int i = 0; i <= V; ++i)G[i].clear();
}
//加边
void Add_Edge(int from, int to, int cap, int cost) {
G[from].push_back(edge(to, cap, cost, G[to].size()));
G[to].push_back(edge(from, 0, -cost, G[from].size() - 1));
}
//flow是自己传进去的变量,就是最后的最大流,返回的是最小费用
int Min_cost_max_flow(int s, int t, int f, int& flow) {
int res = 0; fill(H, H + 1 + V, 0);
while (f) {
priority_queue <pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > q;
fill(dis, dis + 1 + V, INF);
dis[s] = 0; q.push(pair<int, int>(0, s));
while (!q.empty()) {
pair<int, int> now = q.top(); q.pop();
int v = now.second;
if (dis[v] < now.first)continue;
for (int i = 0; i < G[v].size(); ++i) {
edge& e = G[v][i];
if (e.capacity > 0 && dis[e.to] > dis[v] + e.cost + H[v] - H[e.to]) {
dis[e.to] = dis[v] + e.cost + H[v] - H[e.to];
PreV[e.to] = v;
PreE[e.to] = i;
q.push(pair<int, int>(dis[e.to], e.to));
}
}
}
if (dis[t] == INF)break;
for (int i = 0; i <= V; ++i)H[i] += dis[i];
int d = f;
for (int v = t; v != s; v = PreV[v])d = min(d, G[PreV[v]][PreE[v]].capacity);
f -= d; flow += d; res += d*H[t];
for (int v = t; v != s; v = PreV[v]) {
edge& e = G[PreV[v]][PreE[v]];
e.capacity -= d;
G[v][e.rev].capacity += d;
}
}
return res;
}
int Max_cost_max_flow(int s, int t, int f, int& flow) {
int res = 0;
fill(H, H + 1 + V, 0);
while (f) {
priority_queue <pair<int, int> > q;
fill(dis, dis + 1 + V, -INF);
dis[s] = 0;
q.push(pair<int, int>(0, s));
while (!q.empty()) {
pair<int, int> now = q.top(); q.pop();
int v = now.second;
if (dis[v] > now.first)continue;
for (int i = 0; i < G[v].size(); ++i) {
edge& e = G[v][i];
if (e.capacity > 0 && dis[e.to] < dis[v] + e.cost + H[v] - H[e.to]) {
dis[e.to] = dis[v] + e.cost + H[v] - H[e.to];
PreV[e.to] = v;
PreE[e.to] = i;
q.push(pair<int, int>(dis[e.to], e.to));
}
}
}
if (dis[t] == -INF)break;
for (int i = 0; i <= V; ++i)H[i] += dis[i];
int d = f;
for (int v = t; v != s; v = PreV[v])d = min(d, G[PreV[v]][PreE[v]].capacity);
f -= d; flow += d;
res += d*H[t];
for (int v = t; v != s; v = PreV[v]) {
edge& e = G[PreV[v]][PreE[v]];
e.capacity -= d;
G[v][e.rev].capacity += d;
}
}
return res;
}
};
Min_Cost_Max_Flow MCMF;
char str[200][200];
struct Point{
int x,y;
}men[maxn],hourse[maxn];
int cal(int a,int b){
return abs(men[a].x-hourse[b].x)+abs(men[a].y-hourse[b].y);
}
int main()
{
int n,m;
while(scanf("%d %d",&n,&m)&&(n+m)){
int len1 = 0,len2 =0;
getchar();
for(int i=1;i<=n;i++){
scanf("%s",str[i]+1);
for(int j=1;j<=m;j++){
if(str[i][j]=='m')men[++len1].x = i,men[len1].y = j;
else if(str[i][j]=='H')hourse[++len2].x = i,hourse[len2].y = j;
}
getchar();
}
int s = 0,t = len1+len2+1;
MCMF.Init(t);
for(int i=1;i<=len1;i++){
for(int j=1;j<=len2;j++){
MCMF.Add_Edge(i,j+len1,1,cal(i,j));
}
}
for(int i=1;i<=len1;i++)MCMF.Add_Edge(s,i,1,0);
for(int i=1;i<=len2;i++)MCMF.Add_Edge(i+len1,t,1,0);
int flow = 0;
printf("%d\n",MCMF.Min_cost_max_flow(s,t,INF,flow));
// printf(" %d\n",flow);
}
return 0;
}
更多推荐
已为社区贡献6条内容
所有评论(0)