POJ---3308-Paratroopers(最大流)

本文描述了一个战争背景下的防御问题,地球军队需要使用激光枪同时消灭来自火星的伞兵,通过构建数学模型并运用最短路径算法,寻找安装激光枪的最优方案以最小化成本。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

F - Paratroopers
Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

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 <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <iostream>
#include <algorithm>
#define min_def(x,y) x<y?x:y
using namespace std;
const int N = 105;
const double MAX = 1000000.0;
struct Edge{
    int a,b,next;
    double val;
} edge[15*N];
int head_edge;
int head[N];
int deep[N];
int first,end;
void addedge(int a,int b,double val){
    edge[head_edge].a=a;
    edge[head_edge].b=b;
    edge[head_edge].val=val;
    edge[head_edge].next=head[a];
    head[a]=head_edge++;
    edge[head_edge].a=b;
    edge[head_edge].b=a;
    edge[head_edge].val=0.0;
    edge[head_edge].next=head[b];
    head[b]=head_edge++;
}
int BFS(){
   // cout<<first<<'\12';
    queue <int> lll;
    memset(deep,-1,sizeof(deep));
    deep[first]=0;
    lll.push(first);
    while(!lll.empty()){
        int now=lll.front();
        lll.pop();
        for(int i=head[now]; i!=-1; i=edge[i].next)
        {
            int poi=edge[i].b;
            if(deep[poi]==-1 && edge[i].val>0)
            {
              //  cout<<poi<<"\n";
                deep[poi]=deep[now]+1;
                lll.push(poi);
            }
        }
    }
   // cout<<end<<" "<<deep[end]<<'\12';
    //getchar();
    return deep[end] != -1;
}
double dfs(int star,double V){
    double r=0;
    if(star==end)return V;
    for(int i=head[star]; i!=-1 && r<V; i=edge[i].next){
        int poi=edge[i].b;
        if(edge[i].val>0 && deep[poi]==deep[star]+1){
            double x=min(edge[i].val, V-r);
            x=dfs(poi,x);
            r+=x;
            edge[i].val -=x;
            edge[i^1].val +=x;
        }
    }
    if(!r)deep[star]=-2;
    return r;
}
double dinic(){
    double total=0.0,t;
    int i=0;
    while(BFS()){
        while(t=dfs(first,MAX))
           total+=t;
           //cout<<i++<<'\12';
    }
    return total;
}
int main(){
    int T,i,n,m,k;
    double v;
    scanf("%d",&T);
    while(T--){
        head_edge=0;
        memset(head,-1,sizeof(head));
        memset(deep,-1,sizeof(deep));
        scanf("%d%d%d",&n,&m,&k);
        first=0;
        end=m+n+1;
        for(i=1; i<=n; ++i){
            scanf("%lf",&v);
            addedge(0,i,log(v));
        }
        for(i=1; i<=m; ++i){
            scanf("%lf",&v);
            addedge(i+n,end,log(v));
        }
        int aa,bb;
        for(i=1; i<=k; ++i){
            scanf("%d%d",&aa,&bb);
            addedge(aa,n+bb,MAX);
          //  cout<<"this\n";
        }
        double ans=dinic();
        printf("%.4lf\n",exp(ans));
    }
    return 0;
}
/*
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
*/










评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值