BZOJ1834 [ZJOI2010]network 网络扩容

187 篇文章 0 订阅
9 篇文章 0 订阅

标签:最大流,费用流

题目

题目传送门

Description
给定一张有向图,每条边都有一个容量C和一个扩容费用W。这里扩容费用是指将容量扩大1所需的费用。求: 1、 在不扩容的情况下,1到N的最大流; 2、 将1到N的最大流增加K所需的最小扩容费用。
Input
输入文件的第一行包含三个整数N,M,K,表示有向图的点数、边数以及所需要增加的流量。 接下来的M行每行包含四个整数u,v,C,W,表示一条从u到v,容量为C,扩容费用为W的边。
Output
输出文件一行包含两个整数,分别表示问题1和问题2的答案。
Sample Input
5 8 2

1 2 5 8

2 5 9 9

5 1 6 2

5 1 1 8

1 2 8 7

2 5 4 9

1 2 1 1

1 4 2 1

Sample Output
13 19

30%的数据中,N<=100

100%的数据中,N<=1000,M<=5000,K<=10

HINT

Source

Day1

分析

第一问是最大流的模板题

第二问需要用最小费用最大流的算法,建立超级源点(0),从超级源点向1号点建立一条容量为k,费用为0的边,再在原来的所有边上都新开一条费用为w,容量为∞的边,对这个图进行最小费用最大流的算法

code

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#define rep(i,a,b) for(register int i=a;i<=b;i++)
#define dep(i,a,b) for(register int i=a;i>=b;i--)
#define reg(x) for(int i=last[x];i;i=e[i].next)
#define ll long long
#define mem(x,num) memset(x,num,sizeof(x))
#define inf 0x7fffffff
using namespace std;
inline ll read()
{
    ll f=1,x=0;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;
}
const int maxn=1e4+6,maxq=1e5+6;
struct edge{int to,from,v,t,next,c;}e[maxn<<1];
int n,m,k,cnt=1,ans,last[maxn],from[maxn],que[maxq],h[maxq],dis[maxn],inq[maxn];

bool bfs()
{
    int head=0,tail=0,now;
    mem(h,-1);que[0]=1,h[1]=0;
    while(head<=tail){
        now=que[head++];
        reg(now)
            if(e[i].v&&h[e[i].to]==-1){h[e[i].to]=h[now]+1;que[++tail]=e[i].to;}
    }
    if(h[n]==-1)return 0;else return 1;
}

int dfs(int x,int f)
{
    if(x==n)return f;
    int w,used=0;
    reg(x)
        if(e[i].v&&h[e[i].to]==h[x]+1){
            w=f-used;w=dfs(e[i].to,min(w,e[i].v));
            e[i].v-=w;e[i^1].v+=w;
            used+=w;if(used==f)return f;
        }
    if(!used)h[x]=-1;
    return used;
}
void dinic(){while(bfs())ans+=dfs(1,inf);}

void build()
{
    int t=cnt;
    rep(i,2,t)
        if(i%2==0){
        e[++cnt]=(edge){e[i].to,e[i].from,inf,0,last[e[i].from],e[i].t};last[e[i].from]=cnt;
        e[++cnt]=(edge){e[i].from,e[i].to,0,0,last[e[i].to],-e[i].t};last[e[i].to]=cnt;}
}
bool spfa()
{
    int head=0,tail=0,now;
    rep(i,0,n)dis[i]=inf;
    que[0]=dis[0]=0;inq[0]=1;
    while(head<=tail){
        now=que[head++];
        reg(now)
            if(e[i].v&&dis[now]+e[i].c<dis[e[i].to]){
                dis[e[i].to]=dis[now]+e[i].c;from[e[i].to]=i;
                if(!inq[e[i].to]){que[++tail]=e[i].to;inq[e[i].to]=1;}
            }
        inq[now]=0;
    }
    if(dis[n]==inf)return 0;
    return 1;
}
void mcf()
{
    int x=inf;
    for(int i=from[n];i;i=from[e[i].from])x=min(x,e[i].v);
    for(int i=from[n];i;i=from[e[i].from]){e[i].v-=x;e[i^1].v+=x;ans+=x*e[i].c;}
}

int main()
{
    n=read(),m=read(),k=read();
    rep(i,1,m){
        int u=read(),v=read(),w=read(),c=read();
        e[++cnt]=(edge){v,u,w,c,last[u]};last[u]=cnt;
        e[++cnt]=(edge){u,v,0,-c,last[v]};last[v]=cnt;
    }
    dinic();
    cout<<ans<<' ';
    ans=0;build();
    e[++cnt]=(edge){1,0,k,0,last[0],0};last[0]=cnt;
    while(spfa())mcf();
    cout<<ans<<endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值