[网络流]2279. 网络战争

题目:2279. 网络战争

分析:

1、最小化平均值可以考虑二分答案,用以下的不等式判断二分是否合法。
在这里插入图片描述
2、每条边权与枚举的平均值作差后,如果为负,则一定选。
3、对于作差非负的边组成的网络跑最大流,无向图建边双向权值相同。
4、double二分的循环条件要注意

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;//三年竞赛一场空,不开long long见祖宗
//typedef __int128 lll;
#define print(i) cout << "debug: " << i << endl
#define close() ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
#define mem(a, b) memset(a, b, sizeof(a))
#define pb(a) push_back(a)
#define x first
#define y second
typedef pair<int, int> pii;
const double eps = 1e-8;
const ll mod = 1e9 + 7;
const int maxn = 1e3, maxm = 1e5;
const int inf = 0x3f3f3f3f;
struct edge
{
    int ep;
    double val;
    int nex;
    double nowval;
}e[maxm];
int head[maxn], tot;

void init()
{
    mem(head, -1), tot = 0;
}

void addedge(int x, int y, double val)
{
    e[tot] = {y, val, head[x]}, head[x] = tot++;
    e[tot] = {x, val, head[y]}, head[y] = tot++;
}

int n, m, s, t;
int depth[maxn], cur[maxn], q[maxn];

bool bfs()
{
    int hh = 0, tt = 0; q[hh] = s;
    mem(depth, -1), depth[s] = 1;
    cur[s] = head[s];
    while(hh <= tt)
    {
        int now = q[hh++];
        for(int i = head[now]; ~i; i = e[i].nex)
        {
            int ep = e[i].ep;
            if(depth[ep] == -1 && e[i].nowval)
            {
                depth[ep] = depth[now] + 1;
                cur[ep] = head[ep];
                if(ep == t) return true;
                q[++tt] = ep;
            }
        }
    }
    return false;
}

double dfs(int now, double flow)
{
    if(now == t) return flow;
    double res = 0;
    for(int i = cur[now]; ~i && flow; i = e[i].nex)
    {
        int ep = e[i].ep;
        if(depth[ep] == depth[now] + 1 && e[i].nowval)
        {
            double k = dfs(ep, min(flow, e[i].nowval));
            if(k < eps) depth[ep] = -1;
            e[i].nowval -= k, e[i ^ 1].nowval += k;
            res += k, flow -= k;
        }
    }
    return res;
}

double dinic(double c)
{
    double res = 0;
    for(int i = 0; i < tot; i += 2)
    {
        if(e[i].val < c) res += e[i].val - c, e[i].nowval = e[i ^ 1].nowval = 0;
        else e[i].nowval = e[i ^ 1].nowval = e[i].val - c;
    }
    while(bfs())
        res += dfs(s, inf);
    return res;
}

int main()
{
    init();
    cin >> n >> m >> s >> t;
    for(int i = 1; i <= m; i++)
    {
        int x, y, val; cin >> x >> y >> val;
        addedge(x, y, val);
    }
    double l = 1, r = 1e7, mid;
    while(l + eps < r)
    {
        mid = (l + r) / 2;
        if(dinic(mid) < 0) r = mid;
        else l = mid;
    }
    printf("%.2f\n", mid);
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值