bzoj 3130 费用流

比较水的一道题,保证流量最大的边流量最小即可

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<cmath>

#define md
#define ll long long
#define inf 1000000000.0
#define eps 1e-8
#define N 510
#define M 50010
using namespace std;
int m;
struct yts { int x,t,ne; double f;} e[2*M],path[M];
int v[N],head[N],q[N],dep[N];
int S,T,num=1;
double ans=0,mxflow;
int fcmp(double a,double b)
{
if (a-b>eps) return 1;
if (a-b<-eps) return -1;
return 0;
}

void put(int x,int y,double f)
{
num++; e[num].x=x; e[num].t=y; e[num].f=f;
e[num].ne=v[x]; v[x]=num;
}

void add(int x,int y,double f)
{
put(x,y,f); put(y,x,0);
}

bool bfs()
{
int h=0,w=1,x,y; q[1]=S; dep[S]=1;
while (h!=w)
{
x=q[++h];
for (int i=v[x];i;i=e[i].ne)
{
y=e[i].t;
if (fcmp(e[i].f,0)==1&&!dep[y])
{
dep[y]=dep[x]+1;
q[++w]=y;
}
}
}
return dep[T]!=0;
}

double dfs(int x,double f)
{
if (x==T) return f;
double used=0.0,now=0.0;
for (int i=head[x];i;i=e[i].ne)
{
int y=e[i].t;
if (dep[y]==dep[x]+1&&fcmp(e[i].f,0)==1)
{
now=dfs(y,min(f-used,e[i].f));
used+=now; e[i].f-=now; e[i^1].f+=now;
if (fcmp(e[i].f,0)==1) head[x]=i;
if (fcmp(used,f)==0) break;
}
}
if (fcmp(used,0)==0) dep[x]=-1;
return used;
}

void dinic()
{
while (1)
{
memset(dep,0,sizeof(dep));
for (int i=1;i<=T;i++) head[i]=v[i];
if (bfs()) ans+=dfs(S,inf); else break;
}
}

void maxflow()
{
num=1; memset(v,0,sizeof(v));
for (int i=1;i<=m;i++) add(path[i].x,path[i].t,path[i].f);
ans=0.0; dinic(); mxflow=ans;
}

bool ok(double mid)
{
num=1; memset(v,0,sizeof(v));
for (int i=1;i<=m;i++) add(path[i].x,path[i].t,min(path[i].f,mid));
ans=0.0; dinic();
//printf("%.2lf %.2lf\n",ans,mxflow);
return fcmp(ans,mxflow)==0;
}

double solve()
{
double l=0,r=mxflow,mid;
while (r-l>(1e-5))
{
mid=(l+r)/2; //printf("%.2lf %.2lf %.2lf\n",l,mid,r);
if (ok(mid)) r=mid; else l=mid;
}
return l;
}


int main()
{
int n; double p;
scanf("%d%d%lf",&n,&m,&p); S=1; T=n;
for (int i=1;i<=m;i++) scanf("%d%d%lf",&path[i].x,&path[i].t,&path[i].f);
maxflow();
printf("%d\n",(int)mxflow);
printf("%.4lf\n",solve()*p);
return 0;
}


你可能感兴趣的:(bzoj 3130 费用流)