20-2-24-最小费用的最大流-POJ2195

Going Home

Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 28598Accepted: 14193

Description

On a grid map there are n little men and n houses. In each unit time, every little man can move one unit step, either horizontally, or vertically, to an adjacent point. For each little man, you need to pay a $1 travel fee for every step he moves, until he enters a house. The task is complicated with the restriction that each house can accommodate only one little man.

Your task is to compute the minimum amount of money you need to pay in order to send these n little men into those n different houses. The input is a map of the scenario, a ‘.’ means an empty space, an ‘H’ represents a house on that point, and am ‘m’ indicates there is a little man on that point.
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-83udeBfn-1582562898339)(D:\AFiles\TyporaPictures\2195_1.jpg)]
You can think of each point on the grid map as a quite large square, so it can hold n little men at the same time; also, it is okay if a little man steps on a grid with a house without entering that house.

Input

There are one or more test cases in the input. Each case starts with a line giving two integers N and M, where N is the number of rows of the map, and M is the number of columns. The rest of the input will be N lines describing the map. You may assume both N and M are between 2 and 100, inclusive. There will be the same number of 'H’s and 'm’s on the map; and there will be at most 100 houses. Input will terminate with 0 0 for N and M.

Output

For each test case, output one line with the single integer, which is the minimum amount, in dollars, you need to pay.

Sample Input

2 2
.m
H.
5 5
HH..m
.....
.....
.....
mm..H
7 8
...H....
...H....
...H....
mmmHmmmm
...H....
...H....
...H....
0 0

Sample Output

2
10
28

Source

Pacific Northwest 2004

  • 先贴一个正确的代码, 该blog中包含详细的解题思路戳我

  • 自己写的一个wa的代码, 找了半天bug了,反例呢,有时间回来找一下,

#pragma warning(disable:4996)
#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 105;
const int max_house = 105;

struct Edge {
	int from, to, cap, flow, cost;
	Edge(int u = 0, int v = 0, int c = 0, int f = 0, int w = 0) :from(u), to(v), cap(c), flow(f), cost(w)
	{}
};
struct MCMF {
	int n, m;
	vector<Edge>edges;
	vector<int>G[maxn << 1];
	int inq[maxn << 1];
	int d[maxn << 1];
	int p[maxn << 1];//上一条弧
	//a[i]存储搜索到i节点时得到的最小的cap-flow
	int a[maxn << 1];//可改进量
	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 bellmanFloyed(int s, int t, int& flow, long long& cost) {
		for (int i = 0; i < n; i++) {
			d[i] = INF;
		}
		memset(inq, 0, sizeof(inq));
		d[s] = 0;
		inq[s] = true;
		p[s] = 0;
		a[s] = INF;
		queue<int>Q;
		Q.push(s);
		while (!Q.empty()) {
			int u = Q.front();
			Q.pop();
			inq[u] = false;
			for (int i = 0; i < G[u].size(); 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]) {
						Q.push(e.to);
						inq[e.to] = true;
					}
				}
			}
		}
		if (d[t] == INF) {
			return false;
		}
		flow += a[t];
		cost += (long long)a[t] * (long long)d[t];
		for (int u = t; u != s; u = edges[p[u]].from) {
			edges[p[u]].flow += a[t];
			//cost += (long long)a[t] * (long long)edges[p[u]].cost;
		}
		return true;
	}
	int minCostMaxFlow(int s, int t, long long& cost) {
		int flow = 0;
		cost = 0;
		while (bellmanFloyed(s, t, flow, cost));
		return flow;
	}
};


int n, m;
int g[maxn][maxn];
int main() {
	while (scanf("%d%d", &n, &m) && (n || m)) {
		getchar();
		//存储第i个房子的位置, 从0开始存储, 房子的位置从1开始
		int sit[maxn][2];
		int k = 0;
		//房子和人的数量
		int num_of_from = 0;

		char c;
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				c = getchar();
				if (c == '.') {
					g[i + 1][j + 1] = 0;
				}
				else if (c == 'm') {
					g[i + 1][j + 1] = 1;
					num_of_from++;
				}
				else {
					g[i + 1][j + 1] = 2;
					sit[k][0] = i + 1;
					sit[k][1] = j + 1;
					k++;
				}
			}
			getchar();
		}
		MCMF mcmf;
		mcmf.init(num_of_from * 2 + 2);
		//当前人顶点的序号
		int num = 1;
		//每个人到房子建立一个容量为1, cost为两者距离的边
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				if (g[i + 1][j + 1] == 1) {
					for (int p = 0; p < k; p++) {
						mcmf.addEdge(num, num_of_from + 1 + p, 1, abs(j + 1 - sit[p][1]) + abs(i + 1 - sit[p][0]));
					}
					num++;
				}
			}
		}
		//原点到每个人建立一个容量为1, 费用为0的边
		for (int i = 0; i < num_of_from; i++) {
			mcmf.addEdge(0, i + 1, 1, 0);
		}
		//每个房子到汇点建立一个容量为1,费用为0的边
		for (int i = 0; i < num_of_from; i++) {
			mcmf.addEdge(num_of_from + i + 1, num_of_from * 2 + 1, 1, 0);
		}
		long long cost = 0;
		//cout << mcmf.edges.size() << endl;
		//cout << num_of_from << endl;
		/*for (int i = 0; i < mcmf.edges.size(); i++) {
			if(i%2==0)
				cout << mcmf.edges[i].from << " " << mcmf.edges[i].to << " " << mcmf.edges[i].cap << endl;
		}*/
		mcmf.minCostMaxFlow(0, num_of_from * 2 + 1, cost);
		cout << cost << "\n";
	}
	return 0;
}
/*
2 2
.m
H.
5 5
HH..m
.....
.....
.....
mm..H
7 8
...H....
...H....
...H....
mmmHmmmm
...H....
...H....
...H....
0 0
*/
/*
2
10
28
*/#pragma warning(disable:4996)
#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 105;
const int max_house = 105;

struct Edge {
	int from, to, cap, flow, cost;
	Edge(int u = 0, int v = 0, int c = 0, int f = 0, int w = 0) :from(u), to(v), cap(c), flow(f), cost(w)
	{}
};
struct MCMF {
	int n, m;
	vector<Edge>edges;
	vector<int>G[maxn << 1];
	int inq[maxn << 1];
	int d[maxn << 1];
	int p[maxn << 1];//上一条弧
	//a[i]存储搜索到i节点时得到的最小的cap-flow
	int a[maxn << 1];//可改进量
	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 bellmanFloyed(int s, int t, int& flow, long long& cost) {
		for (int i = 0; i < n; i++) {
			d[i] = INF;
		}
		memset(inq, 0, sizeof(inq));
		d[s] = 0;
		inq[s] = true;
		p[s] = 0;
		a[s] = INF;
		queue<int>Q;
		Q.push(s);
		while (!Q.empty()) {
			int u = Q.front();
			Q.pop();
			inq[u] = false;
			for (int i = 0; i < G[u].size(); 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]) {
						Q.push(e.to);
						inq[e.to] = true;
					}
				}
			}
		}
		if (d[t] == INF) {
			return false;
		}
		flow += a[t];
		//cost += (long long)a[t] * (long long)d[t];
		for (int u = t; u != s; u = edges[p[u]].from) {
			edges[p[u]].flow += a[t];
			cost += (long long)a[t] * (long long)edges[p[u]].cost;
		}
		return true;
	}
	int minCostMaxFlow(int s, int t, long long& cost) {
		int flow = 0;
		cost = 0;
		while (bellmanFloyed(s, t, flow, cost));
		return flow;
	}
};


int n, m;
int g[maxn][maxn];
int main() {
	while (scanf("%d%d", &n, &m) && (n || m)) {
		getchar();
		//存储第i个房子的位置, 从0开始存储, 房子的位置从1开始
		int sit[maxn][2];
		int k = 0;
		//房子和人的数量
		int num_of_from = 0;

		char c;
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				c = getchar();
				if (c == '.') {
					g[i + 1][j + 1] = 0;
				}
				else if (c == 'm') {
					g[i + 1][j + 1] = 1;
					num_of_from++;
				}
				else {
					g[i + 1][j + 1] = 2;
					sit[k][0] = i + 1;
					sit[k][1] = j + 1;
					k++;
				}
			}
			getchar();
		}
		MCMF mcmf;
		mcmf.init(num_of_from * 2 + 2);
		//当前人顶点的序号
		int num = 1;
		//每个人到房子建立一个容量为1, cost为两者距离的边
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				if (g[i + 1][j + 1] == 1) {
					for (int p = 0; p < k; p++) {
						mcmf.addEdge(num, num_of_from + 1 + p, 1, abs(j + 1 - sit[p][1]) + abs(i + 1 - sit[p][0]));
					}
					num++;
				}
			}
		}
		//原点到每个人建立一个容量为1, 费用为0的边
		for (int i = 0; i < num_of_from; i++) {
			mcmf.addEdge(0, i + 1, 1, 0);
		}
		//每个房子到汇点建立一个容量为1,费用为0的边
		for (int i = 0; i < num_of_from; i++) {
			mcmf.addEdge(num_of_from + i + 1, num_of_from * 2 + 1, 1, 0);
		}
		long long cost = 0;
		//cout << mcmf.edges.size() << endl;
		//cout << num_of_from << endl;
		/*for (int i = 0; i < mcmf.edges.size(); i++) {
			if(i%2==0)
				cout << mcmf.edges[i].from << " " << mcmf.edges[i].to << " " << mcmf.edges[i].cap << endl;
		}*/
		mcmf.minCostMaxFlow(0, num_of_from * 2 + 1, cost);
		cout << cost << "\n";
	}
	return 0;
}
/*
2 2
.m
H.
5 5
HH..m
.....
.....
.....
mm..H
7 8
...H....
...H....
...H....
mmmHmmmm
...H....
...H....
...H....
0 0
*/
/*
2
10
28
*/

  • 重写了代码, 换个板子
#pragma warning(disable:4996)
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<queue>
#include<cstdio>
#include<string>
#include<algorithm>
const int INF = 0x3f3f3f3f;
using namespace std;
const int maxn = 1005;
const int maxm = 105 * 105 << 2;
struct Edge {
	int to, next;
	int flow, cost, cap;
}edge[maxm];

int num_of_edges, head[maxn];

void addEdge(int u, int v, int cap, int cost) {
	edge[num_of_edges].to = v;
	edge[num_of_edges].cap = cap;
	edge[num_of_edges].cost = cost;
	edge[num_of_edges].flow = 0;
	edge[num_of_edges].next = head[u];
	head[u] = num_of_edges++;

	edge[num_of_edges].to = u;
	edge[num_of_edges].cap = 0;
	edge[num_of_edges].cost = -cost;
	edge[num_of_edges].flow = 0;
	edge[num_of_edges].next = head[v];
	head[v] = num_of_edges++;
}

bool inq[maxn];
int dis[maxn];
int pre[maxn];
queue<int>q;
bool spfa(int source, int target) {
	memset(inq, 0, sizeof inq);
	memset(dis, 0x3f, sizeof dis);
	memset(pre, 0xff, sizeof pre);

	dis[source] = 0;
	inq[source] = true;
	q.push(source);
	while (!q.empty()) {
		int u = q.front();
		q.pop();
		inq[u] = false;
		for (int e = head[u]; e != -1; e = edge[e].next) {
			int v = edge[e].to;
			if (edge[e].cap > edge[e].flow&& dis[v] > dis[u] + edge[e].cost) {
				dis[v] = dis[u] + edge[e].cost;
				pre[v] = e;
				if (!inq[v]) {
					inq[v] = true;
					q.push(v);
				}
			}
		}
	}
	return pre[target] != -1;
}
int minCostMaxFlow(int source, int target, int& cost, int& flow)
{
	while (spfa(source, target)) {
		int minn = INF;
		for (int i = pre[target]; i != -1; i = pre[edge[i ^ 1].to]) {
			int remain = edge[i].cap - edge[i].flow;
			minn = min(minn, remain);
		}
		for (int i = pre[target]; i != -1; i = pre[edge[i ^ 1].to]) {
			edge[i].flow += minn;
			edge[i ^ 1].flow -= minn;
			cost += minn * edge[i].cost;
		}
		flow += minn;
	}
	return flow;
}

int n, m;
struct point {
	int x, y;
}sit_of_houses[maxn], sit_of_men[maxn];

int main() {
	while (scanf("%d%d", &n, &m) && (n || m)) {
		num_of_edges = 0;
		memset(head, 0xff, sizeof head);
		char str[110];
		int num_of_houses = 0, num_of_men = 0;

		for (int i = 0; i < n; i++) {
			scanf("%s", str);
			for (int j = 0; j < m; j++) {
				if (str[j] == 'H') {
					sit_of_houses[num_of_houses].x = i;
					sit_of_houses[num_of_houses].y = j;
					num_of_houses++;
				}
				else if (str[j] == 'm') {
					sit_of_men[num_of_men].x = i;
					sit_of_men[num_of_men].y = j;
					num_of_men++;
				}
			}
		}

		int source = 0;
		int target = num_of_houses + num_of_men + 1;

		//人指向房子添加边
		for (int i = 0; i < num_of_men; i++) {
			for (int j = 0; j < num_of_houses; j++) {
				int w = abs(sit_of_men[i].x - sit_of_houses[j].x) + abs(sit_of_men[i].y - sit_of_houses[j].y);
				//添加Cap为1, cost为w的边
				addEdge(i + 1, num_of_men + j + 1, 1, w);
			}
		}
		//0号源点,和汇点之间的连边
		for (int i = 0; i < num_of_men; i++) {
			addEdge(0, i + 1, 1, 0);
			addEdge(i + num_of_men + 1, target, 1, 0);
		}

		int cost = 0, flow = 0;
		minCostMaxFlow(source, target, cost, flow);
		printf("%d\n", cost);
	}
}
/*
2 2
.m
H.
5 5
HH..m
.....
.....
.....
mm..H
7 8
...H....
...H....
...H....
mmmHmmmm
...H....
...H....
...H....
0 0
*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值