您的位置:首页 > 其它

bzoj 3597: [Scoi2014]方伯伯运椰子 spfa判负环+分数规划

2017-07-21 16:39 423 查看

题意

给出一张每条边都是满流的残余网络,每条边都有容量,费用,扩容费用和缩容费用。要求在不改变总流量且每条边仍然为满流的前提下求最大平均费用减少。

n<=5000

分析

我们把这张图看做是费用流的残余网络,那么如果这不是最小费用流,那么必然可以找到从某一个点开始最后回到它自己的增广路。

由于这题是求平均,有一个显然的结论就是我们只用增广一个负环。可以先分数规划一下,那么问题就变成了新图中是否含有负环。用dfs实现的spfa来判负环即可。

代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;

const int N=5005;
const double eps=1e-3;

int n,m,cnt,last
,ins
;
double dis
;
struct edge{int to,next,w;double val;}e[N*2];

int read()
{
int x=0,f=1;char ch=getchar();
while (ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}

void addedge(int u,int v,int w)
{
e[++cnt].to=v;e[cnt].w=w;e[cnt].next=last[u];last[u]=cnt;
}

bool dfs(int x)
{
ins[x]=1;
for (int i=last[x];i;i=e[i].next)
if (dis[x]+e[i].val<dis[e[i].to])
{
if (ins[e[i].to]) return 1;
dis[e[i].to]=dis[x]+e[i].val;
if (dfs(e[i].to)) return 1;
}
ins[x]=0;
return 0;
}

bool check(double mid)
{
for (int i=1;i<=cnt;i++) e[i].val=mid-e[i].w;
for (int i=1;i<=n+2;i++) dis[i]=0,ins[i]=0;
for (int i=1;i<=n+2;i++) if (dfs(i)) return 1;
return 0;
}

int main()
{
n=read();m=read();
for (int i=1;i<=m;i++)
{
int u=read(),v=read(),a=read(),b=read(),c=read(),d=read();
if (u==n+1) continue;
addedge(u,v,-b-d);
if (c) addedge(v,u,d-a);
}
double l=0,r=1e9;
while (r-l>eps)
{
double mid=(l+r)/2;
if (check(mid)) l=mid;
else r=mid;
}
printf("%.2lf",l);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: