本文主要是介绍poj 2195 bfs+有流量限制的最小费用流,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
题意:
给一张n * m(100 * 100)的图,图中” . " 代表空地, “ M ” 代表人, “ H ” 代表家。
现在,要你安排每个人从他所在的地方移动到家里,每移动一格的消耗是1,求最小的消耗。
人可以移动到家的那一格但是不进去。
解析:
先用bfs搞出每个M与每个H的距离。
然后就是网络流的建图过程了,先抽象出源点s和汇点t。
令源点与每个人相连,容量为1,费用为0;
汇点与每个家相连,容量为1,费用为0;
然后让每个人与家相连,容量为1,费用为距离。
以此求一个流量为人数的最小费用流,就行了。
代码:
#pragma comment(linker, "/STACK:1677721600")
#include <map>
#include <set>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <cassert>
#include <iostream>
#include <algorithm>
#define pb push_back
#define mp make_pair
#define LL long long
#define lson lo,mi,rt<<1
#define rson mi+1,hi,rt<<1|1
#define Min(a,b) ((a)<(b)?(a):(b))
#define Max(a,b) ((a)>(b)?(a):(b))
#define mem0(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define mem(a,b) memset(a,b,sizeof(a))
#define FIN freopen("in.txt", "r", stdin)
#define FOUT freopen("out.txt", "w", stdout)using namespace std;
const int mod = 1e9 + 7;
const double eps = 1e-8;
const double ee = exp(1.0);
const int inf = 0x3f3f3f3f;
const int maxn = 100 + 10;
const int maxv = 100 * 100 + 10;
const double pi = acos(-1.0);
const LL iinf = 0x3f3f3f3f3f3f3f3f;int readT()
{char c;int ret = 0,flg = 0;while(c = getchar(), (c < '0' || c > '9') && c != '-');if(c == '-') flg = 1;else ret = c ^ 48;while( c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c ^ 48);return flg ? - ret : ret;
}//firs - 最短距离 second 顶点编号
typedef pair<int, int> P;struct Edge
{int to, cap, cost, rev;Edge() {}Edge(int _to, int _cap, int _cost, int _rev){to = _to;cap = _cap;cost = _cost;rev = _rev;}
};int V;//顶点数
vector<Edge> g[maxv]; //图的邻接表
int h[maxv]; //残量
int dist[maxv]; //最短距离
int preV[maxv], preE[maxv]; //前驱节点 以及对于的边void init()
{for (int i = 0; i <= V; i++){g[i].clear();}
}//向图中增加一条从fr到to容量为cap费用为cost的边
void addEdge(int fr, int to, int cap, int cost)
{g[fr].push_back(Edge(to, cap, cost, g[to].size()));g[to].push_back(Edge(fr, 0, -cost, g[fr].size() - 1));
}//求解从s到t流量为f的最小费用流
//没有流量为f的流,返回-1
int minCostFlow(int s, int t, int f)
{int res = 0;memset(h, 0, sizeof(h));while (f > 0){priority_queue<P, vector<P>, greater<P> > q;memset(dist, inf, sizeof(dist));dist[s] = 0;q.push(P(0, s));while (!q.empty()){P now = q.top();q.pop();int v = now.second;if (dist[v] < now.first)continue;for (int i = 0; i < g[v].size(); i++){Edge& e = g[v][i];if (e.cap > 0 && dist[e.to] > dist[v] + e.cost + h[v] - h[e.to]){dist[e.to] = dist[v] + e.cost + h[v] - h[e.to];preV[e.to] = v;preE[e.to] = i;q.push(P(dist[e.to], e.to));}}}if (dist[t] == inf){return -1;}for (int v = 0; v < V; v++)h[v] += dist[v];int d = f;for (int v = t; v != s; v = preV[v]){d = min(d, g[preV[v]][preE[v]].cap);}f -= d;res += d * h[t];for (int v = t; v != s; v = preV[v]){Edge& e = g[preV[v]][preE[v]];e.cap -= d;g[v][e.rev].cap += d;}}return res;
}int dir[][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};char graph[maxn][maxn];
int n, m;
int step[maxv>>1][maxn][maxn];bool ok(int x, int y)
{return 0 <= x && x < n && 0 <= y && y < m;
}void bfs(int x, int y, int id)
{mem1(step[id]);//x <-> yqueue<P> q;q.push(mp(x, y));step[id][x][y] = 0;while (!q.empty()){P now = q.front();q.pop();int x = now.first;int y = now.second;for (int i = 0; i < 4; i++){int nx = x + dir[i][0];int ny = y + dir[i][1];if (ok(nx, ny)){if (step[id][nx][ny] == -1){step[id][nx][ny] = step[id][x][y] + 1;q.push(mp(nx, ny));}}}}
}void debug(int id)
{for (int k = 0; k < id; k++){for (int i = 0; i < n; i++){for (int j = 0; j < n; j++){printf("%d ", step[k][i][j]);}cout << endl;}cout << "---------------" <<endl;}
}vector<P> home;int main()
{
#ifdef LOCALFIN;
#endif // LOCALwhile (~scanf("%d%d", &n, &m)){if (!n && !m)break;for (int i = 0; i < n; i++)scanf("%s", graph[i]);home.clear();int manNum = 0;init();for (int i = 0; i < n; i++){for (int j = 0; j < m; j++){if (graph[i][j] == 'm'){bfs(i, j, manNum);manNum++;}if (graph[i][j] == 'H'){home.pb(mp(i, j));}}}int homeNum = home.size();
// debug(manNum);int s = manNum + homeNum;int t = s + 1;V = t + 1;for (int i = 0; i < manNum; i++){addEdge(s, i, 1, 0);}for (int i = 0; i < homeNum; i++){addEdge(i + manNum, t, 1, 0);}for (int i = 0; i < manNum; i++){for (int j = 0; j < homeNum; j++){int x = home[j].first;int y = home[j].second;addEdge(i, j + manNum, 1, step[i][x][y]);}}printf("%d\n", minCostFlow(s, t, manNum));}return 0;
}
这篇关于poj 2195 bfs+有流量限制的最小费用流的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!