zoukankan      html  css  js  c++  java
  • 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
    ", exp(EK()));    //恢复
    
        }
    
    
        return 0;
    }
    
  • 相关阅读:
    [转] websocket新版协议分析+python实现 & websocket 通信协议
    [转] html5演示
    新浪网内部讲师进阶研习会之笔记
    css3 animation属性
    【转】python多线程编程
    搭建selenium自动化环境步骤
    win10下搭建QTP测试环境
    cocos2dx跨平台环境
    Cocos2dx运行win32工程
    原型模式(prototype)
  • 原文地址:https://www.cnblogs.com/Inkblots/p/5717162.html
Copyright © 2011-2022 走看看