(最小割)poj 3308 Paratroopers

Paratroopers
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 7915 Accepted: 2379

Description

It is year 2500 A.D. and there is a terrible war between the forces of the Earth and the Mars. Recently, the commanders of the Earth are informed by their spies that the invaders of Mars want to land some paratroopers in the × n grid yard of one their main weapon factories in order to destroy it. In addition, the spies informed them the row and column of the places in the yard in which each paratrooper will land. Since the paratroopers are very strong and well-organized, even one of them, if survived, can complete the mission and destroy the whole factory. As a result, the defense force of the Earth must kill all of them simultaneously after their landing.

In order to accomplish this task, the defense force wants to utilize some of their most hi-tech laser guns. They can install a gun on a row (resp. column) and by firing this gun all paratroopers landed in this row (resp. column) will die. The cost of installing a gun in theith row (resp. column) of the grid yard is ri (resp. ci ) and the total cost of constructing a system firing all guns simultaneously is equal to the product of their costs. Now, your team as a high rank defense group must select the guns that can kill all paratroopers and yield minimum total cost of constructing the firing system.

Input

Input begins with a number T showing the number of test cases and then, T test cases follow. Each test case begins with a line containing three integers 1 ≤ m ≤ 50 , 1 ≤ n ≤ 50 and 1 ≤ l ≤ 500 showing the number of rows and columns of the yard and the number of paratroopers respectively. After that, a line with m positive real numbers greater or equal to 1.0 comes where the ith number is ri and then, a line with n positive real numbers greater or equal to 1.0 comes where the ith number is ci. Finally, l lines come each containing the row and column of a paratrooper.

Output

For each test case, your program must output the minimum total cost of constructing the firing system rounded to four digits after the fraction point.

Sample Input

1
4 4 5
2.0 7.0 5.0 2.0
1.5 2.0 2.0 8.0
1 1
2 2
3 3
4 4
1 4

Sample Output

16.0000

Source


分析:最小割。总费用用乘法算,故用对数函数log()转化成加法。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <vector>
#include <set>
#include <algorithm>
using namespace std;
#define mem(x,i)	memset(x,i,sizeof(x))
#define sfi(a)		scanf("%d", &a)  
#define sfii(a,b)	scanf("%d %d", &a, &b)  
#define sfiii(a,b,c)scanf("%d %d %d", &a, &b, &c)  
const double EPS = 1e-10;
const double pai = acos(-1.0);
const int INF = 0xfffffff;
const int MOD = 1000000007;
typedef long long LL;
const int maxn = +5;
--------------------最大流Dinic开始------------------------
const int maxnode = 105;
const int maxedge = 10005*4;
struct Edge
{
	int v, nxt;
	double f;
};
struct Dinic{
	int src, sink;
	int g[maxnode + 10];
	int nume;
	Edge e[maxedge * 2 + 10];
	void init()
	{
		memset(g, 0, sizeof(g));
		nume = 1;
	}
	void addedge(int u, int v, double c)
	{
		e[++nume].v = v;
		e[nume].f = c;
		e[nume].nxt = g[u];
		g[u] = nume;
		e[++nume].v = u;
		e[nume].f = 0;
		e[nume].nxt = g[v];
		g[v] = nume;
	}

	queue<int> que;
	bool vis[maxnode + 10];
	int dist[maxnode + 10];
	void bfs()
	{
		memset(dist, 0, sizeof(dist));
		while (!que.empty())	que.pop();
		vis[src] = true;
		que.push(src);
		while (!que.empty()){
			int u = que.front();
			que.pop();
			for (int i = g[u]; i; i = e[i].nxt)
			if (e[i].f>EPS&&!vis[e[i].v]){
				que.push(e[i].v);
				dist[e[i].v] = dist[u] + 1;
				vis[e[i].v] = true;
			}
		}
	}
	double dfs(int u, double delta)
	{
		if (u == sink){
			return delta;
		}
		else{
			double ret = 0;
			for (int i = g[u]; delta&&i; i = e[i].nxt)
			if (e[i].f&&dist[e[i].v] == dist[u] + 1){
				double dd = dfs(e[i].v, min(e[i].f, delta));
				e[i].f -= dd;
				e[i ^ 1].f += dd;
				delta -= dd;
				ret += dd;
			}
			return ret;
		}
	}

	double maxflow(int s, int t)
	{
		src = s;  sink = t;
		double ret = 0;
		while (true){
			memset(vis, 0, sizeof(vis));
			bfs();
			if (!vis[sink]) return ret;
			ret += dfs(src, INF);
		}
	}
};
--------------------Dinic结束------------------------
Dinic D;
int N, M, L;
int main()
{
	//freopen("f:\\input.txt", "r", stdin);
	int T;
	sfi(T);
	while (T--){
		sfiii(N, M, L);
		D.init();
		int s = N + M, t = s + 1;
		for (int i = 0; i < N; i++){
			double c;
			scanf("%lf", &c);
			c = log(c);
			D.addedge(s, i, c);
		}
		for (int i = 0; i < M; i++){
			double c;
			scanf("%lf", &c);
			c = log(c);
			D.addedge(N + i, t, c);
		}
		for (int i = 0; i < L; i++){
			int x, y;
			sfii(x, y);
			D.addedge(x - 1, y - 1 + N, INF);
		}
		double ret = D.maxflow(s, t);
		printf("%.4f\n", exp(ret));
	}
	return 0;
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值