题目描述:
给出一个流量网络,给出参数费用 T
题目分析:
Bob/肯定会吧全部的T放在最大流量边上,所以我们必须让最大流量边最小
先跑最大流,然后二分最大流量边,看新的网络是否最大流等于原来网络的最大流
题目链接:
Ac 代码:
#include <cstdio>
#include <iostream>
#include <queue>
#include <cstring>
#include <cmath>
#define il inline
using namespace std;
const int eps=1e-8;
const int inf=0x7fffffff;
const int maxm=2000;
int head[1100],to[maxm*2],net[maxm*2],deep[1100],cnt=1;
double cap[maxm*2],p,cx[maxm],d;
il void add(int x,int y,double c){cnt++,to[cnt]=y,cap[cnt]=c,net[cnt]=head[x],head[x]=cnt;}
queue <int> dl;
int n,m;
int s[maxm],t[maxm];
il bool BFS(int s,int t)
{
while(!dl.empty()) dl.pop();
memset(deep,-1,sizeof(deep));
dl.push(s),deep[s]=0;
while(!dl.empty())
{
int x=dl.front();dl.pop();
for(int i=head[x];i;i=net[i])
if(cap[i]>0&&deep[to[i]]==-1)
dl.push(to[i]),deep[to[i]]=deep[x]+1;
}
return deep[t]==-1?0:1;
}
double dfs(int now,double flow,int t)
{
if(now==t) return flow;
double w,used=0;
for(int i=head[now];i;i=net[i])
{
int v=to[i];
if(deep[v]==deep[now]+1&&cap[i])
{
w=dfs(v,min(flow-used,cap[i]),t);
cap[i]-=w;
cap[i^1]+=w;
used+=w;
if(used==flow) return flow;
}
}
if(!used) deep[now]=-1;
return used;
}
il double dinic(int s,int t)
{
double maxflow=0;
while(BFS(s,t)) maxflow+=dfs(s,inf,t);
return maxflow;
}
inline void adx(int x,int y,double cax)
{
add(x,y,cax),add(y,x,0);
}
il bool comp(double x,double y)
{
double t=x-y;
if(t<0) t=-t;
if(t<=1e-9) return 1;
return 0;
}
il bool check(double mid)
{
memset(head,0,sizeof(head)),cnt=1;
for(int i=1,u,v;i<=m;i++)
adx(s[i],t[i],std::min(mid,cx[i]));
double now=dinic(1,n);
//printf("%.0lf %.0lf %.0lf\n",now,mid,d);
return comp(now,d);
}
int main()
{
double c;
scanf("%d%d%lf",&n,&m,&p);
for(int i=1,u,v;i<=m;i++)
{
scanf("%d%d%lf",&s[i],&t[i],&cx[i]);
adx(s[i],t[i],cx[i]);
}
d=dinic(1,n);
double l=0,r=50000;
while(!comp(r-l,eps))
{
double mid=(l+r)*0.5;
bool x=check(mid);
//printf("%d\n",x);
if(x) r=mid;
else l=mid;
//printf("%.4lf %.4lf\n",l,r);
}
printf("%.0lf %.4lf\n",d,l*p);
//printf("%d\n",check(0));
return 0;
}