### 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.

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

16.0000

### Source

Amirkabir University of Technology Local Contest 2006

### Solution

1. 指定超级源S=0,超级汇T=n+m+1
2. 前m个点分别与S连边,边权为ri ; 之后n个点分别于T连边,边权为ci
3. 根据每个点给出的坐标(r,c)给对应的$r\rightarrow c$连边,边权为INF

### Code

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cmath>
#define maxn 210
#define maxe 5100
#define INF 0x3f3f3f3f
//#define DEBUG
using namespace std;
int m,n,L,cnt,S,T;

struct edge
{
int to;
int next;
double val;
}e[maxe];

{
char ch;
do
ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-');
if(ch=='-') sign=-1,ch=getchar();
while(ch>='0'&&ch<='9')
{
ch=getchar();
}
}

inline void ins(int u,int v,double w)
{
cnt++;
e[cnt].to=v;
e[cnt].val=w;
}

inline void insert(int u,int v,double w)
{ ins(u,v,w);ins(v,u,0); }

void init()
{
memset(e,0,sizeof(e));
S=0,T=n+m+1,cnt=1;
}

void build_graph()
{
for(int i=1;i<=n;++i)
{
double w;
scanf("%lf",&w);
insert(S,i,log(w));
}
for(int i=1;i<=m;++i)
{
double w;
scanf("%lf",&w);
insert(i+n,T,log(w));
}
for(int i=1;i<=L;++i)
{
insert(u,v+n,INF);
}
}

bool bfs()
{
memset(dis,-1,sizeof(dis));
queue<int> q;
q.push(0);
dis[0]=0;
while(!q.empty())
{
int u=q.front();q.pop();
while(i)
{
if(e[i].val&&dis[e[i].to]<0)
{
dis[e[i].to]=dis[u]+1;
q.push(e[i].to);
}
i=e[i].next;
}
}
if(dis[T]==-1) return 0;
return 1;
}

double dfs(int x,double flow)
{
if(x==T) return flow;
double used=0,w;
while(i)
{
if(e[i].val&&dis[e[i].to]==dis[x]+1)
{
w=flow-used;
w=dfs(e[i].to,min(w,e[i].val));
e[i].val-=w;
e[i^1].val+=w;
used+=w;
if(used==flow) return flow;
}
i=e[i].next;
}
if(!used) dis[x]=-1;
return used;
}

double dinic()
{
double ans=0;
while(bfs()) ans+=dfs(0,INF);
return ans;
}

int main()
{
while(T--)
{
init();
build_graph();
#ifdef DEBUG
for(int i=1;i<=cnt;++i)
{
printf("%d-->to=%d,next=%d,val=%lf\n",i,e[i].to,e[i].next,e[i].val);
}
#endif
printf("%.4lf\n",exp(dinic()));
}
return 0;
}


Responses