codeforces 653D (二分 最大流)

D. Delivery Bears
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Niwel is a little golden bear. As everyone knows, bears live in forests, but Niwel got tired of seeing all the trees so he decided to move to the city.

In the city, Niwel took on a job managing bears to deliver goods. The city that he lives in can be represented as a directed graph with nnodes and m edges. Each edge has a weight capacity. A delivery consists of a bear carrying weights with their bear hands on a simple path from node 1 to node n. The total weight that travels across a particular edge must not exceed the weight capacity of that edge.

Niwel has exactly x bears. In the interest of fairness, no bear can rest, and the weight that each bear carries must be exactly the same. However, each bear may take different paths if they like.

Niwel would like to determine, what is the maximum amount of weight he can deliver (it's the sum of weights carried by bears). Find the maximum weight.

Input

The first line contains three integers nm and x (2 ≤ n ≤ 501 ≤ m ≤ 5001 ≤ x ≤ 100 000) — the number of nodes, the number of directed edges and the number of bears, respectively.

Each of the following m lines contains three integers aibi and ci (1 ≤ ai, bi ≤ nai ≠ bi1 ≤ ci ≤ 1 000 000). This represents a directed edge from node ai to bi with weight capacity ci. There are no self loops and no multiple edges from one city to the other city. More formally, for each i and j that i ≠ j it's guaranteed that ai ≠ aj or bi ≠ bj. It is also guaranteed that there is at least one path from node 1 to node n.

Output

Print one real value on a single line — the maximum amount of weight Niwel can deliver if he uses exactly x bears. Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.

Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct if .

Examples
input
4 4 3
1 2 2
2 4 1
1 3 1
3 4 2
output
1.5000000000
input
5 11 23
1 2 3
2 3 4
3 4 5
4 5 6
1 3 4
2 4 5
3 5 6
1 4 2
2 5 3
1 5 2
3 2 30
output
10.2222222222
Note

In the first sample, Niwel has three bears. Two bears can choose the path , while one bear can choose the path . Even though the bear that goes on the path  can carry one unit of weight, in the interest of fairness, he is restricted to carry 0.5 units of weight. Thus, the total weight is 1.5 units overall. Note that even though Niwel can deliver more weight with just 2 bears, he must use exactly 3 bears on this day.

题意:给定一个图,有k只熊,每只熊都携带相同的重量,每条边的权值表示最多能够承受多少重量,求熊最多能够负重多少.

二分结果,然后每条边的边权改成在目前的情况下最多能够承受多少只熊,然后跑一跑最大流.

坑:注意精度,一开始用eps来判断结束不是wa就是t,后来改成了二分的次数就好了.

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
const long long INF = 1e8;
#define maxn 55
#define maxm 1111
#define eps 1e-10

long long n, m, x, N;
long long s, t;
struct Edge
{
    long long from, to,next,cap,flow;
    void get(long long u,long long a,long long b,long long c,long long d)
    {
        from = u; to = a; next = b; cap = c; flow = d;
    }
}edge[maxm];
long long tol;
long long head[maxn];
long long gap[maxn],dep[maxn],pre[maxn],cur[maxn];
void init()
{
    tol=0;
    memset(head,-1,sizeof(head));
}

void add_edge(long long u,long long v,long long w,long long rw=0)
{ //cout << u << " " << v << " " << w << endl;
    edge[tol].get(u, v,head[u],w,0);head[u]=tol++;
    edge[tol].get(v, u,head[v],rw,0);head[v]=tol++;
}
long long sap(long long start,long long end,long long N)
{
    memset(gap,0,sizeof(gap));
    memset(dep,0,sizeof(dep));
    memcpy(cur,head,sizeof(head));
    long long u=start;
    pre[u]=-1;
    gap[0]=N;
    long long ans=0;
    while(dep[start]<N)
    {
        if(u==end)
        {
            long long Min=INF;
            for(long long i=pre[u];i!=-1;i=pre[edge[i^1].to])
                if(Min>edge[i].cap-edge[i].flow)
                   Min=edge[i].cap-edge[i].flow;
            for(long long i=pre[u];i!=-1;i=pre[edge[i^1].to])
            {
                edge[i].flow+=Min;
                edge[i^1].flow-=Min;
            }
            u = start;
            ans+=Min;
            continue;
        }
        bool flag=false;
        long long v;
        for(long long i=cur[u];i !=-1;i=edge[i].next)
        {
            v=edge[i].to;
            if(edge[i].cap-edge[i].flow&&dep[v]+1==dep[u])
            {
                flag=true;
                cur[u]=pre[v]=i;
                break;
            }
        }
        if(flag)
        {
            u=v;
            continue;
        }
        long long Min=N;
        for(long long i=head[u];i!=-1;i=edge[i].next)
            if(edge[i].cap-edge[i].flow&&dep[edge[i].to]<Min)
        {
            Min=dep[edge[i].to];
            cur[u]=i;
        }
        gap[dep[u]]--;
        if(!gap[dep[u]]) return ans;
        dep[u]=Min+1;
        gap[dep[u]]++;
        if(u!=start) u=edge[pre[u]^1].to;
    }
    return ans;
}

long long e[511][3];

bool ok (double num) {//总重量为num的情况下能不能完成
    double per = num/x;//每只熊
    init ();
    s = 0, t = n-1, N = n;
    for (long long i = 1; i <= m; i++) {
        long long u = e[i][0]-1, v = e[i][1]-1, w = e[i][2];
        long long cap = floor (1.0*w/per);
        add_edge (u, v, cap);
    }
    long long ans = sap (s, t, N);
    return ans >= x;
}

int main () {
    //freopen ("in.txt", "r", stdin);
    scanf ("%lld%lld%lld", &n, &m, &x);
    for (long long i = 1; i <= m; i++) {
        scanf ("%lld%lld%lld", &e[i][0], &e[i][1], &e[i][2]);
    }
    double l = 0.0, r = 1e12;
    int k = 200;
    while (k--) {
        double mid = (l+r)/2.0;
        if (ok (mid))
            l = mid;
        else
            r = mid;
    }
    printf ("%.10f\n", ok (r) ? r : l);
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值