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 m × 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
/*
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;
}