【最小割】POJ-3308 Paratroopers

34 篇文章 0 订阅
30 篇文章 0 订阅
Paratroopers
Time Limit: 1000MS Memory Limit: 65536K
   

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 the ith 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
————————————————————脑残的分割线————————————————————
前言:数学差就是伤。
思路:根据最小割的思路。每一行都放置一个激光炮就是一组解,每一列都放置也是一组解。因此把每一行当做一个顶点排在第一列和S相连,每一列当做顶点排在第二列和T相连。伞兵就是使得某行和某列产生关系的要素。
如此以来,轻松得到所有的割。
这时候发现,总费用是乘积,这可把我难坏了……
后来看到Discuss,只需要把权值设为Log(x),对数相加,真数相乘!~数学就是伤。化乘为加。
P.S. 另外要注意,判断实数是否为0,采用 d > eps。这里eps可以取1e-8。也就是0.00000001。
代码如下:
/*
ID: j.sure.1
PROG:
LANG: C++
*/
/****************************************/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <string>
#include <climits>
#include <iostream>
#define LL long long
using namespace std;
const int INF = 0x3f3f3f3f;
const double eps = 1e-8;
/****************************************/
const int N = 222, M = 5555;
int tot, head[N], cur[N], q[N], s[N], S, T, lev[N];
struct Node {
	int u, v;
	double cap;
	int next;
}edge[M];

void init()
{
	tot = 0; memset(head, -1, sizeof(head));
}

void add(int u, int v, double c)
{
	edge[tot].u = u; edge[tot].v = v; edge[tot].cap = c;
	edge[tot].next = head[u]; head[u] = tot++;
}

bool bfs()
{
	memset(lev, -1, sizeof(lev));
	int fron = 0, rear = 0;
	q[rear++] = S; lev[S] = 0;
	while(fron < rear) {
		int u = q[fron%N]; fron++;
		for(int i = head[u]; i != -1; i = edge[i].next) {
			int v = edge[i].v;
			if(edge[i].cap > eps && lev[v] == -1) {
				lev[v] = lev[u] + 1;//标号
				q[rear%N] = v; rear++;
				if(v == T) return true;
			}
		}
	}
	return false;
}

double Dinic()
{
	double ret = 0;
	while(bfs()) {
		memcpy(cur, head, sizeof(head));
		int u = S, top = 0;
		while(1) {
			if(u == T) {
				double mini = 1.0*INF;
			    int	loc;
				for(int i = 0; i < top; i++) {
					if(mini > edge[s[i]].cap) {
						mini = edge[s[i]].cap;
						loc = i;
					}
				}
				for(int i = 0; i < top; i++) {
					edge[s[i]].cap -= mini;
					edge[s[i]^1].cap += mini;
				}
				ret += mini;
				top = loc;
				u = edge[s[top]].u;
			}
			int &i = cur[u];
			for(; i != -1; i = edge[i].next) {
				int v = edge[i].v;
				if(edge[i].cap > eps && lev[v] == lev[u] + 1) break;
			}
			if(i != -1) {
				s[top++] = i;
				u = edge[i].v;
			}
			else {
				if(!top) break;
				lev[u] = -1;//阻塞流
				u = edge[s[--top]].u;
			}
		}
	}
	return ret;
}

int main()
{
#ifdef J_Sure
//	freopen("000.in", "r", stdin);
//	freopen(".out", "w", stdout);
#endif
	int kase;
	scanf("%d", &kase);
	int m, n, l;
	while(kase--) {
		scanf("%d%d%d", &m, &n, &l);
		S = 0; T = m + n + 1;
		double x;
		init();
		for(int i = 1; i <= m; i++) {
			scanf("%lf", &x);
			add(S, i, log(x)); add(i, S, 0);
		}
		for(int i = 1; i <= n; i++) {
			scanf("%lf", &x);
			add(i+m, T, log(x)); add(T, i+m, 0);
		}
		int u, v;
		for(int i = 1; i <= l; i++) {
			scanf("%d%d", &u, &v);
			add(u, v+m, 1.0*INF); add(v+m, u, 0);
		}
		double ret = Dinic();
		ret = exp(ret);//e的ret次方
		printf("%.4f\n", ret);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值