bzoj1834: [ZJOI2010]network 网络扩容

题目传送门点我

border="0" width="330" height="86" src="http://music.163.com/outchain/player?type=2&id=28377210&auto=1&height=66">

**

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


**

Solution

**
调了好久QAQ……脑残了……

第一问就是求最大流 直接套最大流模板

然后考虑一下第二问“把最大流扩大k”

我的思路是这样完善的:(神犇们不要喷我…)

枚举每一条边?额……瞬间秒掉这种思想QAQ
==》
最小费用诶…觉得很像最小费用问题呢…(沉思一会…)貌似就是一个最小费用问题QAQ…但是最大流怎么限制呢?
==》
再加一个原点和1连一条容量为k的边不就好了!
bingo~

然后我很213的调了半天……最后发现了一个很213的错误QAQ


**

Code

**

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn=1010,maxm=5010,inf=2000000000;

struct data{int to,from,next,v,c;}e[maxm*4];
int n,m,k,ans,maxflow,q[maxn],head[maxn],inq[maxn],from[maxn],dis[maxn],h[maxn],cnt=1;

void ins(int u,int v,int w,int c)
{
    cnt++;
    e[cnt].to=v;e[cnt].from=u;
    e[cnt].next=head[u];head[u]=cnt;
    e[cnt].v=w;e[cnt].c=c;
}
void insert(int u,int v,int w,int c)
{
    ins(u,v,w,c);ins(v,u,0,-c);
}

bool bfs()
{
    int t=0,w=1;
    memset(h,-1,sizeof(h));
    q[0]=1;h[1]=0;
    while(t<w)
    {
        int x=q[t++];if(t==maxn)t=0;
        for(int i=head[x];i;i=e[i].next)
            if(e[i].c==0&&e[i].v&&h[e[i].to]<0)
            {
                q[w++]=e[i].to;if(w==maxn)w=0;
                h[e[i].to]=h[x]+1;
            }
    }
    return h[n]!=-1;
}
int dfs(int x,int f)
{
    if(x==n)return f;
    int w,used=0;
    for(int i=head[x];i;i=e[i].next)
        if(e[i].c==0&&e[i].v&&h[e[i].to]==h[x]+1)
        {
            w=dfs(e[i].to,min(f-used,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())
        maxflow+=dfs(1,inf);
}
bool spfa()
{
    int t=0,w=1;
    for(int i=1;i<=n;i++)dis[i]=inf;
    inq[0]=1;q[0]=0;dis[0]=0;
    while(t<w)
    {
        int x=q[t++];if(t==maxn)t=0;
        for(int i=head[x];i;i=e[i].next)
            if(e[i].v>0&&dis[x]+e[i].c<dis[e[i].to])
            {
                dis[e[i].to]=dis[x]+e[i].c;
                from[e[i].to]=i;
                if(!inq[e[i].to])
                {
                    inq[e[i].to]=1;q[w++]=e[i].to;if(w==maxn)w=0;
                }
            }
        inq[x]=0;
    }
    return dis[n]!=inf;
}
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()
{
    scanf("%d%d%d",&n,&m,&k);
    for(int i=1 ; i<=m; i++)
    {
        int u,v,c,w;
        scanf("%d%d%d%d",&u,&v,&c,&w);
        insert(u,v,c,0);
        insert(u,v,inf,w);
    }
    dinic();
    printf("%d",maxflow);
    ins(0,1,k,0);
    ins(1,0,maxflow,0);
    while(spfa())mcf();
    printf(" %d",ans);
    return 0; 
}
/*
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

*/ 


——既然选择了远方,便只顾风雨兼程

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值