Language:
Paratroopers
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 |
题意
m*n的网格上有伞兵降落 在某行或某列上安装激光枪来杀死该行(该列)的伞兵
在第i行安装激光枪的费用为Ri 在第i列安装激光枪的费用为Ci
要求同时开火 杀死所有的伞兵 总的费用为这些激光枪费用的乘积 要使得整个的费用最小
思路 超级源点s 超级汇点t s与每行R之间建边权值为费用
每列C与t之间建边 权值为费用
每个伞兵降落的左边 行与列之间建边 权值为INF
根据割的性质 源点与汇点之间必不连通
割边集为S->R与C->T的集合 也就是选中了行或列
此时求得的最小割即为话费最小的方案
又要求 总的费用为各激光枪费用的乘积 可以借助 对数运算来把乘法转化为加法
#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string.h>
#include <string>
#define MEM(a,x) memset(a,x,sizeof a)
#define eps 1e-8
#define MOD 10009
#define MAXN 110
#define INF 99999999
#define ll __int64
#define bug cout<<"here"<<endl;
#define fread freopen("ceshi.txt","r",stdin)
#define fwrite freopen("out.txt","w",stdout)
using namespace std;
struct Node
{
double c,f;
}map[MAXN][MAXN];
int pre[MAXN];//pre[i]为增广路径顶点i前一个顶点的符号
int queue[MAXN];//数组模拟队列
int s,t;
bool BFS()
{
int cur,qs,qe;//队列当前节点 队列头 队列尾
MEM(pre,-1);
pre[s]=s;
qs=0;qe=1;
queue[qs]=s;
while(qs<qe)
{
cur=queue[qs++];
for(int i=0;i<=t;i++)
{
if(pre[i]==-1&&map[cur][i].c-map[cur][i].f>0)
{
queue[qe++]=i;
pre[i]=cur;
if(i==t) return 1;//汇点在层次网络中
}
}
}
return 0;
}
double maxflow()
{
double max_flow=0,min;
while(BFS())
{
min=INF;
for(int i=t;i!=s;i=pre[i])//调整网络
{
if(map[pre[i]][i].c-map[pre[i]][i].f<min)
min=map[pre[i]][i].c-map[pre[i]][i].f;
}
for(int i=t;i!=s;i=pre[i])
{
map[pre[i]][i].f+=min;
map[i][pre[i]].f-=min;
}
max_flow+=min;
}
return max_flow;
}
int main()
{
// fread;
int tc;
scanf("%d",&tc);
while(tc--)
{
int m,n,l;
scanf("%d%d%d",&m,&n,&l);
s=0; t=m+n+1;
MEM(map,0);
for(int i=1;i<=m;i++)
{
double r;
scanf("%lf",&r);
map[s][i].c=log(r);
}
for(int i=1;i<=n;i++)
{
double c;
scanf("%lf",&c);
map[i+m][t].c=log(c);
}
for(int i=1;i<=l;i++)
{
int u,v;
scanf("%d%d",&u,&v);
map[u][v+m].c=INF;
}
// for(int i=0;i<=m+n+1;i++)
// {
// for(int j=0;j<=m+n+1;j++)
// cout<<i<<" "<<j<<" "<<map[i][j].c<<" "<<map[i][j].f<<endl;
// }
printf("%.4lf\n",exp(maxflow()));
}
return 0;
}