uva 10746(最大流最小费)

题意:有n个银行m个警察,给出每个警察到每个银行花费的时间,问最小花费时间平均是多少。

题解:直接建图+最大流最小费模板。

#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
const int N = 100;
const int INF = 0x3f3f3f3f;
int n, m, p[N];
int cap[N][N], flow[N][N], maxflow;
double d[N], cost[N][N], c;

double ek(int scr, int en) {
	queue<int> q;
	memset(flow, 0, sizeof(flow));
	maxflow = c = 0;
	while (1) {
		bool vis[N];
		memset(vis, 0, sizeof(vis));
		for (int i = 0; i <= en; i++)
			d[i] = INF;
		d[scr] = 0;
		q.push(scr);
		while (!q.empty()) {
			int u = q.front();
			q.pop();
			vis[u] = false;
			for (int v = 0; v <= en; v++) {
				if (cap[u][v] > flow[u][v] && d[v] > d[u] + cost[u][v]) {
					d[v] = d[u] + cost[u][v];
					p[v] = u;
					if (!vis[v]) {
						vis[v] = true;
						q.push(v);
					}
				}
			} 
		} 
		if (d[en] == INF)
			break;
		int a = INF;
		for (int u = en; u != scr; u = p[u])
			a = a < cap[p[u]][u] - flow[p[u]][u] ? a : cap[p[u]][u] - flow[p[u]][u];
		for (int u = en; u != scr; u = p[u]) {
			flow[p[u]][u] += a;
			flow[u][p[u]] -= a;
		}
		c += d[en] * a;
		maxflow += a;
	}
	return c;
}

int main() {
	while (scanf("%d%d", &n, &m) && m + n) {
		memset(cap, 0, sizeof(cap));
		double b;
		for (int i = 1; i <= n; i++)
			for (int j = 1; j <= m; j++) {
				scanf("%lf", &b);
				cap[j][m + i] = 1;
				cost[j][m + i] = b;
				cost[m + i][j] = -b;
			}
		for (int i = 1; i <= m; i++)
			cap[0][i] = 1;
		for (int i = 1; i <= n; i++)
			cap[i + m][n + m + 1] = 1;
		printf("%.2lf\n", ek(0, n + m + 1) / n + 1e-9);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值