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

*/ 


——既然選擇了遠方,便只顧風雨兼程

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章