ACM/ICPC 之 伞兵-最小割转最大流(POJ3308)

 

 

//以行列建点,伞兵位置为单向边-利用对数将乘积转加法
//最小割转最大流
//Time:63Ms Memory:792K
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<queue>
using namespace std;

#define MAXN 105
#define INF 100000
#define EPS 1e-7

int n,m,p;
int s,t;
double res[MAXN][MAXN];
int pre[MAXN];

bool bfs()
{
    memset(pre,-1,sizeof(pre));
    queue<int> q;
    q.push(s);  pre[s] = 0;
    while(!q.empty()){
        int cur = q.front();
        q.pop();
        for(int i = 1; i <= t; i++)
        {
            if(pre[i] == -1 && res[cur][i] > EPS)
            {
                pre[i] = cur;
                if(i == t)  return true;
                q.push(i);
            }
        }
    }
    return false;
}

double EK()
{
    double maxFlow = 0;
    while(bfs()){
        double mind = INF;
        for(int i = t; i != s; i = pre[i])
            mind = min(mind, res[pre[i]][i]);
        for(int i = t; i != s; i = pre[i])
        {
            res[pre[i]][i] -= mind;
            res[i][pre[i]] += mind;
        }
        maxFlow += mind;
    }
    return maxFlow;
}

int main()
{
    //freopen("in.txt", "r", stdin);

    int T;
    scanf("%d",&T);
    while(T--){
        memset(res,0,sizeof(res));
        scanf("%d%d%d", &n,&m,&p);
        s = 0;  t = n + m + 1;
        double c;
        for(int i = 1; i <= n; i++)
        {
            scanf("%lf", &c);
            res[s][i] = log(c); //乘法转为加法
        }
        for(int i = 1; i <= m; i++)
        {
            scanf("%lf", &c);
            res[i+n][t] = log(c);
        }
        for(int i = 1; i <= p; i++)
        {
            int rr,cc;
            scanf("%d%d", &rr,&cc);
            res[rr][cc+n] = INF;
        }

        printf("%.4f\n", exp(EK()));    //恢复

    }


    return 0;
}

 

posted @ 2016-07-29 09:41  文字失效  阅读(...)  评论(... 编辑 收藏