zoj3874 poj3308

Paratroopers

Time Limit: 2 Seconds       Memory Limit: 32768 KB

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 <= 50 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 i-th number is Ci. Finally, llines 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 <cmath>
#include <cstring>
#define MAX 110
#define INF 10000000

struct Node
{
    double c, f;
}map[MAX][MAX];
int pre[MAX];    //pre[i]为增广路径顶点i前一个顶点的序号
int queue[MAX];    //数组模拟队列
int s, t;        //源点;汇点

bool BFS( )    //BFS求增广路
{
    int i, cur, qs, qe;    //队列当前节点;队列头;队列尾
    memset( pre, -1, sizeof(pre) );
    pre[s] = s;
    qs = 0;  qe = 1;
    queue[qs] = s;
    while( qs<qe )
    {
        cur = queue[qs++];
        for( 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;
    int i;
    while( BFS( ) )
    {
        min = INF;
        for( 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( 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( )
{
    int i, j, n, m, l, r, cc, w;
    double c;
    scanf( "%d", &w );
    while( w-- )
    {
        memset( map, 0, sizeof(map) );
        scanf( "%d %d %d", &n, &m, &l );
        s = 0; t = n + m + 1;
        //构建网络;用对数运算来将乘法转换为加法
        for( i = 1; i <= n; i++ )
        {
            scanf( "%lf", &c );  map[s][i].c = log(c);
        }
        for( i = 1; i <= m; i++ )
        {
            scanf( "%lf", &c );  map[i+n][t].c = log(c);
        }
        for( i = 1; i <= l; i++ )
        {
            scanf( "%d %d", &r, &cc );  map[r][n+cc].c = 10000000;
        }
        printf( "%.4lf\n", exp( maxflow() ) );    //输出时将对数值转换为原值
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/Deng1185246160/p/3270449.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值