POJ3308 Paratroopers 二分图+最小点权覆盖+最大流

这题是求的最小花费是个乘积,因此用log先转化为加法,最后再将答案用exp转化。
这道题目将每一个Paratrooper当做一条边,行与列分别当做节点,行与源点相连,容量为相应处理过的花费。列与汇点相连,容量为相应处理过的花费。最后把Paratrooper落下的坐标相应的行和列处理成一条容量为INF的弧。最后求一个最小割。
Paratroopers
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 4848 Accepted: 1470

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<cmath>

using namespace std;

#define mm(a) memset((a),0,sizeof((a)))
#define INF 100000.0
#define eps 1e-8
#define MAXN 220

int n,dis[MAXN],gap[MAXN];
int st,ed;
int row,col,l;
double c[MAXN][MAXN];

int sig(double k)  
{  
	if(fabs(k)<eps)
		return 0;
	return (k>0)?1:-1;  
} 

double sap(int u,double flow)
{
	if(u==ed)
		return flow;
	int i;
	double ans=0,t;
	for(i=0;i<=n+1;++i)
		if(sig(c[u][i])&&dis[u]==dis[i]+1)
		{
			t=sap(i,min(flow-ans,c[u][i]));
			c[u][i]-=t,c[i][u]+=t,ans+=t;
			if(ans==flow)
				return ans;
		}
	if(dis[st]>=n+2)
		return ans;
	if(!--gap[dis[u]])  //gap优化 
		dis[st]=n+2;
	++gap[++dis[u]];
	return ans;
}

void solve()
{
	int x,y;
	double ans=0;
	mm(dis),mm(gap);
	n=row+col;
	for(int i=0;i<=n+1;i++)
		for(int j=0;j<=n+1;j++)
			c[i][j]=0;
	for(int i=1;i<=row;i++)
	{
		scanf("%lf",&c[0][i]);
		c[0][i]=log(c[0][i]);
	}
	for(int i=1;i<=col;i++)
	{
		scanf("%lf",&c[row+i][n+1]);
		c[row+i][n+1]=log(c[row+i][n+1]);
	}
	for(int i=1;i<=l;i++)
	{
		scanf("%d%d",&x,&y);
		c[x][row+y]=INF;
	}
	st=0,ed=n+1;
	for(gap[0]=n+2;dis[st]<n+2;)    
		ans+=sap(st,INF);
	printf("%.4lf\n",exp(ans));
}

int main()
{
	int t;
	cin>>t;
	while(t--)
	{
		scanf("%d%d%d",&row,&col,&l);
		solve();	
	}
	return 0;
} 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值