Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 8516 | Accepted: 2573 |
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大意:
现在要安装激光杀死外星人,每个激光可以消灭掉一行或者一列的所有外星人,但是放置的位置不同,修建一个激光的花费也会不同。
然后给你激光安装在每行和每列的花费,以及所有外星人的落脚点的坐标。现在问你最小花费是多少。
根据最大流最小割定理,此时求的最小割就是花费最小的方案了。
需要注意的是,它问的是所有费用的乘积,因此在算法中可以先取对数,转化成加法计算才行。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <cmath>
using namespace std;
const int MAXN=100+5;
const int inf=1e9;
const double eps=1e-6;
int n,m,k;
int s,e;
double tu[MAXN][MAXN];
int pre[MAXN];
double flow[MAXN];
double EK()
{
int i;
for(i=0;i<=e;++i)pre[i]=-1;
queue<int>q;
q.push(s);
flow[s]=inf*1.0;
while(!q.empty())
{
int u=q.front();
q.pop();
if(u==e)break;
for(i=1;i<=e;++i)
{
if(pre[i]==-1&&tu[u][i]>=eps)
{
pre[i]=u;
flow[i]=min(flow[u],tu[u][i]);
q.push(i);
}
}
}
if(pre[e]==-1)return 0;
else return flow[e];
}
double get_maxflow()
{
double max_flow=0.0;
double flow=EK();
while(flow>=eps)
{
max_flow+=flow;
int u=pre[e],v=e;
while(u!=-1)
{
tu[u][v]-=flow;
tu[v][u]+=flow;
v=u;
u=pre[v];
}
flow=EK();
}
return exp(max_flow);
}
int main()
{
int i;
int t;
scanf("%d",&t);
while(t--)
{
memset(tu,0,sizeof(tu));
scanf("%d%d%d",&n,&m,&k);
s=0;
e=n+m+1;
double x;
for(i=1;i<=n;++i)
{
scanf("%lf",&x);
tu[s][i]=log(x);
}
m+=n;
for(i=n+1;i<=m;++i)
{
scanf("%lf",&x);
tu[i][e]=log(x);
}
int u,v;
while(k--)
{
scanf("%d%d",&u,&v);
v+=n;
tu[u][v]=inf;
}
printf("%.4f\n",get_maxflow());
}
return 0;
}