【SGU 176】 Flow construction

176. Flow construction

time limit per test: 0.5 sec.
memory limit per test: 4096 KB
input: standard
output: standard

You have given the net consisting of nodes and pipes; pipes connect the nodes. Some substance can flow by pipes, and flow speed in any pipe doesn’t exceed capacity of this pipe.
The substance cannot be accumulated in the nodes. But it is being produced in the first node with the non-negative speed and being consumed with the same speed in the last node.
You have some subset taken from the set of pipes of this net. You need to start the motion of substance in the net, and your motion must fully fill the pipes of the given subset. Speed of the producing substance in the first node must be minimal.
Calculate this speed and show the scene of substance motion.
Remember that substance can’t be accumulated in the nodes of the net.

Input
Two positive integer numbers N (1<=N<=100) and M have been written in the first line of the input - numbers of nodes and pipes.
There are M lines follows: each line contains four integer numbers Ui, Vi, Zi, Ci; the numbers are separated by a space. Ui is the beginning of i-th pipe, Vi is its end, Zi is a capacity of i-th pipe (1<=Zi<=10^5) and Ci is 1 if i-th pipe must be fully filled, and 0 otherwise.
Any pair of nodes can be connected only by one pipe. If there is a pipe from node A to node B, then there is no pipe from B to A. Not a single node is connected with itself.
There is no pipe which connects nodes number 1 and N. Substance can flow only from the beginning of a pipe to its end.

Output
Write one integer number in the first line of the output - it ought to be the minimal speed of the producing substance in the first node.
Write M integers in the second line - i-th number ought to be the flow speed in the i-th pipe (numbering of pipes is equal to the input).
If it is impossible to fill the given subset, write “Impossible”.

Sample test(s)

Input
Input 1:
4 4
1 2 2 0
2 4 1 1
1 3 2 1
3 4 3 0
Input 2:
4 4
1 2 1 0
2 4 2 1
1 3 3 1
3 4 2 0

Output
Output 1:
3
1 1 2 2
Output 2:
Impossible

有源汇有上下界的最小流。

详见《有上下界的网络流问题

二分法:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue>
#define M 20005
#define inf 0x3f3f3f3f
using namespace std;
int h[M],s,jud,t,cur[M],tot,d[M],id[M],v[M],n,m,down[M];
struct edge
{
    int from,to,cap,flow,ne;
}E[M];
void Addedge(int from,int to,int cap)
{
    E[++tot]=(edge){from,to,cap,0,h[from]};
    h[from]=tot;
    E[++tot]=(edge){to,from,0,0,h[to]};
    h[to]=tot;
}
int bfs()
{
    for (int i=s;i<=t;i++)
        v[i]=0;
    queue<int> q;
    q.push(s);
    v[s]=1;
    d[s]=0;
    while (!q.empty())
    {
        int x=q.front();
        q.pop();
        for (int i=h[x];i;i=E[i].ne)
        {
            edge e=E[i];
            if (!v[e.to]&&e.cap>e.flow)
            {
                v[e.to]=1;
                d[e.to]=d[x]+1;
                q.push(e.to);
            }
        }
    }
    return v[t];
}
int dfs(int x,int a)
{
    if (x==t||!a) return a;
    int flow=0;
    for (int &i=cur[x];i;i=E[i].ne)
    {
        edge &e=E[i];
        if (d[e.to]!=d[x]+1) continue;
        int f=dfs(e.to,min(e.cap-e.flow,a));
        if (f>0)
        {
            flow+=f;
            a-=f;
            e.flow+=f;
            E[i^1].flow-=f;
            if (!a) break;
        }
    }
    return flow;
}
int dinic()
{
    int flow=0;
    while (bfs())
    {
        for (int i=s;i<=t;i++)
            cur[i]=h[i];
        flow+=dfs(s,inf);
    }
    return flow;
}
int ok()
{
    return jud==dinic();
}
int main()
{
    while (scanf("%d%d",&n,&m)!=EOF)
    {
    jud=0;
    s=0,t=n+1;
    tot=1;
    for (int i=s;i<=t;i++)
        h[i]=0;
    for (int i=1;i<=m;i++)
    {
        id[i]=0;
        int x,y,z,c;
        scanf("%d%d%d%d",&x,&y,&z,&c);
        if (c) down[i]=z,jud+=z,Addedge(s,y,z),Addedge(x,t,z);
        else down[i]=0,Addedge(x,y,z),id[i]=tot-1;
    }
    int l=0,r=inf,ans=inf;
    Addedge(n,1,0);
    while (l<=r)
    {
        for (int i=0;i<=tot;i++)
            E[i].flow=0;
        int mid=(l+r)>>1;
        E[tot-1].cap=mid;
        if (ok()) ans=mid,r=mid-1;
        else l=mid+1;
    }
    if (ans==inf) puts("Impossible");
    else
    {
        for (int i=0;i<=tot;i++)
            E[i].flow=0;
        E[tot-1].cap=ans;
        dinic();
        printf("%d\n",ans);
        for (int i=1;i<=m;i++)
            printf("%d%c",E[id[i]].flow+down[i],i==m?'\n':' ');
    }
    }
    return 0;
}

效率高的做法:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue>
#define M 20005
#define inf 0x3f3f3f3f
using namespace std;
int h[M],s,jud,t,cur[M],tot,d[M],id[M],v[M],n,m,down[M];
struct edge
{
    int from,to,cap,flow,ne;
}E[M];
void Addedge(int from,int to,int cap)
{
    E[++tot]=(edge){from,to,cap,0,h[from]};
    h[from]=tot;
    E[++tot]=(edge){to,from,0,0,h[to]};
    h[to]=tot;
}
int bfs()
{
    for (int i=s;i<=t;i++)
        v[i]=0;
    queue<int> q;
    q.push(s);
    v[s]=1;
    d[s]=0;
    while (!q.empty())
    {
        int x=q.front();
        q.pop();
        for (int i=h[x];i;i=E[i].ne)
        {
            edge e=E[i];
            if (!v[e.to]&&e.cap>e.flow)
            {
                v[e.to]=1;
                d[e.to]=d[x]+1;
                q.push(e.to);
            }
        }
    }
    return v[t];
}
int dfs(int x,int a)
{
    if (x==t||!a) return a;
    int flow=0;
    for (int &i=cur[x];i;i=E[i].ne)
    {
        edge &e=E[i];
        if (d[e.to]!=d[x]+1) continue;
        int f=dfs(e.to,min(e.cap-e.flow,a));
        if (f>0)
        {
            flow+=f;
            a-=f;
            e.flow+=f;
            E[i^1].flow-=f;
            if (!a) break;
        }
    }
    return flow;
}
int dinic()
{
    int flow=0;
    while (bfs())
    {
        for (int i=s;i<=t;i++)
            cur[i]=h[i];
        flow+=dfs(s,inf);
    }
    return flow;
}
int main()
{
    while (scanf("%d%d",&n,&m)!=EOF)
    {
    jud=0;
    s=0,t=n+1;
    tot=1;
    for (int i=s;i<=t;i++)
        h[i]=0;
    for (int i=1;i<=m;i++)
    {
        id[i]=0;
        int x,y,z,c;
        scanf("%d%d%d%d",&x,&y,&z,&c);
        if (c) down[i]=z,jud+=z,Addedge(s,y,z),Addedge(x,t,z);
        else down[i]=0,Addedge(x,y,z),id[i]=tot-1;
    }
    int flow=dinic();
    Addedge(n,1,inf);
    flow+=dinic();
    if (flow!=jud) puts("Impossible");
    else
    {
        dinic();
        printf("%d\n",E[tot-1].flow);
        for (int i=1;i<=m;i++)
            printf("%d%c",E[id[i]].flow+down[i],i==m?'\n':' ');
    }
    }
    return 0;
}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值