Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 8724 | Accepted: 2641 |
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
Source
——————————————————————————————————
给出一个矩阵,要求以最低花费覆盖掉矩阵中的所有点,覆盖规则是,可以一次选择一整行覆盖,或者一整列覆盖,(覆盖每行,每列都对应的有不同的花费),最终的花费是这些行和列花费的乘积。
建图:行列拆开点,有交点就连边,求最小割,这里因为要求的是乘积,不是加和,所以需要用log( a )+log( b ) = log( a * b )转化;
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
using namespace std;
#define LL long long
const int INF = 0x3f3f3f3f;
#define MAXN 500
struct node
{
int u, v, next;
double cap;
} edge[MAXN*MAXN];
int nt[MAXN], s[MAXN], d[MAXN], visit[MAXN];
int cnt;
void init()
{
cnt = 0;
memset(s, -1, sizeof(s));
}
void add(int u, int v, double c)
{
edge[cnt].u = u;
edge[cnt].v = v;
edge[cnt].cap = c;
edge[cnt].next = s[u];
s[u] = cnt++;
edge[cnt].u = v;
edge[cnt].v = u;
edge[cnt].cap = 0;
edge[cnt].next = s[v];
s[v] = cnt++;
}
bool BFS(int ss, int ee)
{
memset(d, 0, sizeof d);
d[ss] = 1;
queue<int>q;
q.push(ss);
while (!q.empty())
{
int pre = q.front();
q.pop();
for (int i = s[pre]; ~i; i = edge[i].next)
{
int v = edge[i].v;
if (edge[i].cap > 0 && !d[v])
{
d[v] = d[pre] + 1;
q.push(v);
}
}
}
return d[ee];
}
double DFS(int x, double exp, int ee)
{
if (x == ee||exp<1e-9) return exp;
double temp,flow=0;
for (int i = nt[x]; ~i ; i = edge[i].next, nt[x] = i)
{
int v = edge[i].v;
if (d[v] == d[x] + 1&&(temp = (DFS(v, min(exp, edge[i].cap), ee))) > 0)
{
edge[i].cap -= temp;
edge[i ^ 1].cap += temp;
flow += temp;
exp -= temp;
if (exp<1e-9) break;
}
}
if (!flow) d[x] = 0;
return flow;
}
double Dinic_flow(int ss, int ee)
{
double ans = 0;
while (BFS(ss, ee))
{
for (int i = 0; i <= ee; i++) nt[i] = s[i];
ans+= DFS(ss, INF, ee);
}
return ans;
}
int main()
{
int T,n,m,k,u,v;
double x;
for(scanf("%d",&T); T--;)
{
init();
scanf("%d%d%d",&m,&n,&k);
for(int i=1; i<=m; i++)
scanf("%lf",&x),add(0,i,log(x));
for(int i=1; i<=n; i++)
scanf("%lf",&x),add(m+i,m+n+1,log(x));
for(int i=0; i<k; i++)
scanf("%d%d",&u,&v),add(u,v+m,INF);
double ans=Dinic_flow(0,m+n+1);
printf("%.4f\n",exp(ans));
}
return 0;
}