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

题解:

最小点权覆盖,最小割模型,可以用最大流去做,然后就是板子题,重点是转换过程,

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#define inf 0x3f3f
#define ll long long
#define MAXN 30000
using namespace std;
int n,m;//点数、边数
int X[MAXN],y[MAXN];
int sp,tp;//原点、汇点
struct node
{
int v,next;
double cap;
}mp[MAXN*10];
int pre[MAXN],dis[MAXN],cur[MAXN];//cur为当前弧优化,dis存储分层图中每个点的层数(即到原点的最短距离),pre建邻接表
int cnt=0;
void init()//不要忘记初始化
{
cnt=0;
memset(pre,-1,sizeof(pre));
}
void add(int u,int v,double w)//加边
{
mp[cnt].v=v;
mp[cnt].cap=w;
mp[cnt].next=pre[u];
pre[u]=cnt++;
mp[cnt].v=u;
mp[cnt].cap=0;
mp[cnt].next=pre[v];
pre[v]=cnt++;
} bool bfs()//建分层图
{
memset(dis,-1,sizeof(dis));
queue<int>q;
while(!q.empty())
q.pop();
q.push(sp);
dis[sp]=0;
int u,v;
while(!q.empty())
{
u=q.front();
q.pop();
for(int i=pre[u];i!=-1;i=mp[i].next)
{
v=mp[i].v;
if(dis[v]==-1&&mp[i].cap>0)
{
dis[v]=dis[u]+1;
q.push(v);
if(v==tp)
break;
}
}
}
return dis[tp]!=-1;
} double dfs(int u,double cap)//寻找增广路
{
if(u==tp||cap==0)
return cap;
double res=0,f;
for(int i=cur[u];i!=-1;i=mp[i].next)
{
int v=mp[i].v;
if(dis[v]==dis[u]+1&&(f=dfs(v,min(cap-res,mp[i].cap)))>0)
{
mp[i].cap-=f;
mp[i^1].cap+=f;
res+=f;
if(res==cap)
return cap;
}
}
if(!res)
dis[u]=-1;
return res;
} double dinic()
{
double ans=0;
while(bfs())
{
for(int i=0;i<=tp;i++)
cur[i]=pre[i];
ans+=dfs(sp,inf);
}
return ans;
}
int main()
{ int _;
scanf("%d",&_);
int l;
while(_--) {
init();
int len=0;
scanf("%d%d%d",&n,&m,&len);
sp=0;tp=n+m+1;
memset(pre,-1,sizeof pre);
cnt=0;
double w;
for (int i = 1; i <=n ; ++i) {
scanf("%lf",&w);
add(sp,i,log(w));
}
for (int i = 1; i <=m ; ++i) {
scanf("%lf",&w);
add(n+i,tp ,log(w));
}
int a,b;
for (int i = 0; i <len ; ++i) {
scanf("%d%d",&a,&b);
add(a,b+n,inf);
} double kk=dinic();
printf("%.4f\n",exp(kk));
}
return 0;
}

  

最新文章

  1. python之路:Day04 --- Python基础4
  2. javascript中的this和e.target的深入研究
  3. Apache
  4. DMG提取安装文件
  5. 武汉科技大学ACM:1007: 不高兴的津津
  6. 动态分析maillog日志,把恶意链接直接用防火墙禁止
  7. C语言程序设计第二次作业—————顺序结构
  8. 网页三剑客:HTML+CSS+JavaScript 之JavaScript
  9. vuejs通过filterBy,orderBy实现搜索筛选,降序排序数据实例
  10. cf1088C Ehab and a 2-operation task (构造)
  11. 树递归写法ref实现
  12. Avizo/Amira应用 - 如何计算面孔率
  13. oozie调度sqoop Job 数据库密码无法保存
  14. 影响MapReduce性能的几个因素
  15. python-mongodb基本操作都在这了
  16. [UE4]RPC,远程调用
  17. 关于webpack下热更新?&amp;自动刷新?的小记(非vue-cli)
  18. HTML5 3D爱心动画及其制作过程
  19. linux 系统信息查看
  20. ppt正文排版

热门文章

  1. UVALive 6261 Jewel heist
  2. Python Day 15 递归、匿名函数、内置函数
  3. 【洛谷P3818】小A和uim之大逃离 II
  4. Android学习笔记_16_添加多个Activity、参数传递、请求码和结果码使用
  5. 简单实现CombineFileInputFormat
  6. Sass 基础(七)
  7. 基于layer封装的异步加载分部视图弹出层
  8. c/c++面试----c工程开发之头文件
  9. 第13届景驰-埃森哲杯广东工业大学ACM程序设计大赛--L-用来作弊的药水
  10. spring cloud 学习之路由网关(zuul)