UVA 10746 - Crime Wave - The Sequel(费用流)

Crime Wave – The Sequel

Input: Standard Input

Output: Standard Output

Time Limit: 2 Seconds

 

n banks have been robbed this fine day. (greater than or equal to n) police cruisers are on duty at various locations in the city. n of the cruisers should be dispatched, one to each of the banks, so as to minimize the average time of arrival at the n banks.

 

Input

The input file contains several sets of inputs. The description of each set is given below:

The first line of input contains 0 < n <= m <= 20n lines follow, each containing m positive real numbers: the travel time for cruiser m to reach bank n.

Input is terminated by a case where m=n=0. This case should not be processed.  

Output

For each set of input output a single number: the minimum average travel time, accurate to 2 fractional digits.

 

Sample Input                             Output for Sample Input

3 4
10.0 23.0 30.0 40.0
5.0 20.0 10.0 60.0
18.0 20.0 20.0 30.0

0 0

13.33



题意:给定n个银行,m个警察,现在每个警察到银行有一个时间,求每个银行都派一个警察看守并且平均时间最少。

思路:最大流最小费用,源点和警察建边,警察和银行建边,银行和汇点建边,然后就是费用流问题。最后输出的时候有个精度问题没注意WA了。

代码:

#include <stdio.h>
#include <string.h>
#include <math.h>
#define INF 0x3f3f3f3f
#include <queue>
using namespace std;
const int N = 55, M = 100005;
const double INFF = 1000000000;

int n, m, D, K, E, first[N], next[M], u[M], v[M], pe[N], pv[N], a[N], f[M], w[M], s, t;
double value, cost[M];
queue<int>q;

void add(int a, int b, int value, double time) {
	u[E] = a; v[E] = b; w[E] = value; cost[E] = time;
	next[E] = first[u[E]];
	first[u[E]] = E ++;
	u[E] = b; v[E] = a; w[E] = 0; cost[E] = -time;
	next[E] = first[u[E]];
	first[u[E]] = E ++;
}

void init() {
	E = s = 0, t = n + m + 1;
	memset(first, -1, sizeof(first));
	for (int i = 1; i <= m; i ++)
		add(0, i, 1, 0.0);
	for (int i = 1; i <= n; i ++) {
		add(i + m, t, 1, 0.0);
		for (int j = 1; j <= m; j ++) {
			scanf("%lf", &value);
			add(j, i + m, 1, value);
		}
	}
}

void solve() {
	init();
	bool vis[N];
	double d[N], C = 0;
	int F = 0;
	memset(f, 0, sizeof(f));
	while(1) {
		for (int i = 0; i <= t; i ++)
			d[i] = INFF;
		d[s] = 0.0;
		memset(vis, 0, sizeof(vis));
		q.push(s);
		while(!q.empty()) {
			int u = q.front(); q.pop();
			vis[u] = 0;
			for (int e = first[u]; e != -1; e = next[e]) {
				if (w[e] > f[e] && d[v[e]] - d[u] - cost[e] > 1e-9) {
					d[v[e]] = d[u] + cost[e];
					pv[v[e]] = u; pe[v[e]] = e;
					if (!vis[v[e]]) {
						vis[v[e]] = 1;
						q.push(v[e]);
					}
				}
			}
		}
		if (fabs(d[t] - INFF) < 1e-9) break;
		int a = INF;
		for (int v = t; v != s; v = pv[v])
			a = min(a, w[pe[v]] - f[pe[v]]);
		for (int v = t; v != s; v = pv[v]) {
			f[pe[v]] += a;
			f[pe[v]^1] -= a;
		}
		C += d[t] * a;
		F += a;
	}
	printf("%.2lf\n", C / n + 0.001);
}

int main() {
	while (~scanf("%d%d", &n, &m) && n + m) {
		solve();
	}
	return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值