BZOJ3130: [Sdoi2013]费用流

题目:http://www.lydsy.com/JudgeOnline/problem.php?id=3130

B要费用最高,那他只要把费用都加在流量最多的那条边上就可以了。

于是题目转化为求最大流中最大边最小。

二分,然后流量是要实数的。。(似乎精度要求并不高。。

#include<cstring>
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<cmath>
#define rep(i,l,r) for (int i=l;i<=r;i++)
#define down(i,l,r) for (int i=l;i>=r;i--)
#define clr(x,y) memset(x,y,sizeof(x))
#define inf 1000000009
#define ll long long
#define maxn 1005
#define eps 1e-5
#define mm 2147483648
#define low(x) (x&(-x))
using namespace std;
struct data{int obj,pre; double c;
}e[maxn*2];
struct node{int x,y;double z;
}q[maxn];
double ans;
int tot=1,n,m,head[maxn],cur[maxn],uu[maxn];
double p,mx;
int read(){
    int x=0,f=1; char ch=getchar();
    while (!isdigit(ch)){if (ch=='-') f=-1; ch=getchar();}
    while (isdigit(ch)){x=x*10+ch-'0'; ch=getchar();}
    return x*f;
}
void insert(int x,int y,double z){
    e[++tot].obj=y; e[tot].c=z; e[tot].pre=head[x]; head[x]=tot;
    e[++tot].obj=x; e[tot].c=0; e[tot].pre=head[y]; head[y]=tot;
}
bool bfs(){
    queue<int > q;
    rep(i,1,n) uu[i]=-1;
    uu[1]=0; q.push(1);
    while (!q.empty()){
        int u=q.front(); q.pop();
        for (int j=head[u];j;j=e[j].pre){
            int v=e[j].obj;
            if (uu[v]==-1&&e[j].c>eps) {
                uu[v]=uu[u]+1; q.push(v);
            }
        }
    }
    if (uu[n]==-1) return 0;
    return 1;
}
double dfs(int x,double mx){
    if (x==n) return mx;
    double used=0;
    for (int j=cur[x];j;j=e[j].pre){
        int v=e[j].obj;
        if (e[j].c>eps&&uu[v]==uu[x]+1){
            double w=dfs(v,min(mx-used,e[j].c));
            used+=w; e[j].c-=w; e[j^1].c+=w;
            if (e[j].c) cur[x]=j;
            if (fabs(used-mx)<eps) return mx;
        }
    }
    if (used<eps) uu[x]=-1;
    return used;
}
double dinic(){
    double ans=0;
    while (bfs()){
        rep(i,1,n) cur[i]=head[i];
        ans=ans+dfs(1,1.0*inf);
    }
    return ans;
}
bool jud(double mid){
    tot=1; clr(head,0);
    rep(i,1,m) insert(q[i].x,q[i].y,min(mid,q[i].z));
    if (fabs(dinic()-ans)>eps) return 0;
    return 1;
}
int main(){
    n=read(); m=read(); scanf("%lf",&p);
    rep(i,1,m){
        q[i].x=read(); q[i].y=read(); scanf("%lf",&q[i].z);
        insert(q[i].x,q[i].y,q[i].z); 
        mx=max(mx,q[i].z);
    }
    ans=dinic();
    printf("%.0lf\n",ans);
    double l=0,r=mx;
    while (abs(r-l)>eps){
        double mid=(l+r)/2;
        if (jud(mid)) r=mid;
        else l=mid;
    }
    printf("%.5lf\n",l*p);
    return 0;
}

 

posted on 2016-01-22 13:33  ctlchild  阅读(168)  评论(0编辑  收藏  举报

导航