【sgu176】Flow construction 有源汇上下界最小流

来源:互联网 发布:sql show tables 编辑:程序博客网 时间:2024/05/21 18:53

Description

  1. 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

[submit]
[forum]
Author: Dmitry Orlov
Resource: Saratov ST team Spring Contest #1
Date: 18.05.2003

Server time: 2016-03-05 03:38:29 Online Contester Team © 2002 - 2016. All rights reserved.

Source


先判断是否有可行流,若有然后清空全图,重建图。

然后按【有源汇上下界可行流】的建图方法,但不建e到s的INF边,然后从超级源到超级汇跑最大流,然后再加上e到s的INF边,然后再跑一遍最大流,流经INF边的流量就是答案。

该算法的思想是在第一步中尽可能填充循环流,以减小最小流的代价。

——lyd

#include<cstdio>#include<iostream>#include<cstring>#include<algorithm>#include<queue>using namespace std;const int INF = 1000000010;const int SZ = 40010;int head[SZ],nxt[SZ],tot = 1;struct edge{    int t,d;}l[SZ];void build(int f,int t,int d){    l[++ tot].t = t;    l[tot].d = d;    nxt[tot] = head[f];    head[f] = tot;}void insert(int f,int t,int d){    build(f,t,d); build(t,f,0);}int deep[SZ];queue<int> q;bool bfs(int s,int e){    memset(deep,0,sizeof(deep));    deep[s] = 1;    while(q.size()) q.pop();    q.push(s);    while(q.size())    {        int f = q.front(); q.pop();        for(int i = head[f];i;i = nxt[i])        {            int v = l[i].t;            if(!deep[v] && l[i].d)            {                deep[v] = deep[f] + 1;                q.push(v);                if(v == e) return true;            }        }    }    return false;}int dfs(int u,int flow,int e){    if(u == e || flow == 0) return flow;    int rest = flow;    for(int i = head[u];i;i = nxt[i])    {        int v = l[i].t;        if(deep[v] == deep[u] + 1 && l[i].d)        {            int f = dfs(v,min(rest,l[i].d),e);            if(f > 0)            {                l[i].d -= f;                l[i ^ 1].d += f;                rest -= f;                if(rest == 0) break;            }            else deep[v] = 0;        }    }    if(flow - rest == 0) deep[u] = 0;    return flow - rest;}int dinic(int s,int e){    int ans = 0;    while(bfs(s,e))    {        int tmp = dfs(s,INF,e);        if(!tmp) break;        ans += tmp;    }    return ans;}int B[SZ];int ff[SZ],tt[SZ],dd[SZ],ee[SZ];int outb[SZ],inb[SZ];void init(){    tot = 1;    memset(inb,0,sizeof(inb));    memset(outb,0,sizeof(outb));    memset(head,0,sizeof(head));}int main(){    int n,m;    scanf("%d%d",&n,&m);    for(int i = 1;i <= m;i ++)    {        scanf("%d%d%d%d",&ff[i],&tt[i],&dd[i],&ee[i]);        if(ee[i] == 0)            insert(ff[i],tt[i],dd[i]);        else            outb[ff[i]] += dd[i], inb[tt[i]] += dd[i],B[i] = dd[i],insert(ff[i],tt[i],0);    }    int s = n + 1,e = n + 2;    int sum = 0;    for(int i = 1;i <= n;i ++)    {        int tmp = inb[i] - outb[i];//      cout<<tmp<<endl;        if(tmp > 0)            insert(s,i,tmp),sum += tmp;        else            insert(i,e,-tmp);    }    insert(n,1,INF);    int tmp = dinic(s,e);   //  printf("%d %d\n",tmp,sum);    if(tmp == sum)    {        init();            for(int i = 1;i <= m;i ++)            {                if(ee[i] == 0)                    insert(ff[i],tt[i],dd[i]);                else                    outb[ff[i]] += dd[i], inb[tt[i]] += dd[i],B[i] = dd[i],insert(ff[i],tt[i],0);            }                   for(int i = 1;i <= n;i ++)            {                int tmp = inb[i] - outb[i];                if(tmp > 0)                    insert(s,i,tmp),sum += tmp;                else                    insert(i,e,-tmp);            }               dinic(s,e);        insert(n,1,INF);        dinic(s,e);        int ans = 0;        for(int i = head[1];i;i = nxt[i])            if(l[i].t == n)                ans = l[i].d;        printf("%d\n",ans);        for(int i = 1;i <= m;i ++)            printf("%d ",B[i] + l[i << 1 | 1].d);    }    else        puts("Impossible");    return 0;}
0 0
原创粉丝点击