No.25 - POJ3308 实数网络流

构造图是关键

一个兵有两种耗费模式,要么Ri开销,要么Cj开销

自然想到源点到中间节点为Ri开销,中间节点到汇点为Cj开销,求最小割。

兵的关系如何表示?

伞兵没有带权值,所以只表示一种行和列的关联关系,所以想到中间节点分为Ri和Cj,每个伞兵链接Ri和Cj,正向边费用为INF,反向边费用为0。

关 于 反 向 边 为 什 么 为 0 \red{关于反向边为什么为0} 0:反向边的作用是修正之前伞兵的分配方式,倘若为INF,则无法起到修正作用。

至于求费用乘积,取个对数即可, 网 络 流 只 能 算 加 法 \red{网络流只能算加法}

// ShellDawn
// POJ3308
// No.25

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<queue>
#include<algorithm>
#define MM(x,y) memset(x,y,sizeof(x))
#define INF 0x3f3f3f3f
using namespace std;

#define maxL 110
int n;
struct Edge{
    int to;
    double v;
    int rvs;
    int pre;
};
Edge E[maxL*maxL];
int cnt = 1;
int pre[maxL];
int V[maxL];
double ans ;

void add(int from,int to,double v,int r){
    E[cnt].v = v;
    E[cnt].to = to;
    E[cnt].rvs = r;
    E[cnt].pre = pre[from];
    pre[from] = cnt++;
}

bool BFS(int s){
    MM(V,0);
    queue<int> q;
    q.push(s);
    V[s] = 1;
    while(!q.empty()){
        int now = q.front();
        q.pop();
        if(V[now] == V[n+1]) continue;
        for(int i=pre[now];i!=0;i=E[i].pre){
            if(E[i].v > 0 && V[E[i].to] == 0){
                V[E[i].to] = V[now]+1;
                q.push(E[i].to);
            }
        }
    }
    if(V[n+1] > 0) return true;
    return false;
}

double DFS(int now,double minflow){
    if(now == n+1) return minflow;
    double flow = 0;
    for(int i=pre[now];i!=0&&flow < minflow;i=E[i].pre){
        if(V[E[i].to] == V[n+1] && E[i].to != n+1) continue;
        if(E[i].v > 0 && V[now] + 1 == V[E[i].to]){
            double f = DFS(E[i].to,min(minflow - flow,E[i].v));
            flow += f;
            E[i].v -= f;
            E[E[i].rvs].v += f;
        }
    }
    if(flow <= 0) V[now] = 0;
    return flow; 
}

void Dinic(){
    ans = 0;
    while(BFS(0)) ans+=DFS(0,(double)INF);
}

int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        MM(pre,0);
        cnt = 1;
        int A,B,C;
        scanf("%d%d%d",&A,&B,&C);
        n = A+B;
        for(int i=1;i<=A;i++){
            double v;
            scanf("%lf",&v);
            add(0,i,log(v),0);
        }
        for(int i=A+1;i<=A+B;i++){
            double v;
            scanf("%lf",&v);
            add(i,n+1,log(v),0);
        }
        for(int i=0;i<C;i++){
            int a,b;
            scanf("%d%d",&a,&b);
            int t = cnt;
            add(a,b+A,(double)INF,t+1);
            add(b+A,a,0,t);
        }
        Dinic();
        printf("%.4f\n",exp(ans));
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值