SGU 176 有源汇有上下界最小流(模板)

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

题意:n个节点m条管道,要求满足每条管道流量的条件下,从源点1到汇点n的最小流量;

解法:

  • 先建一个上下界的图,从超级源点到超级汇点跑一遍。
  • 然后从汇点到源点建一条无上下界的边,在求超级源点到超级汇点的流量,这时的流量为题目所求最小流。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cassert>
using namespace std ;

const int INF = 0x3f3f3f3f ;
const int N = 111 ;
const int M = 1e5 + 11 ;

struct Edge {
    int next , to ;
    int flow , cap ;
    Edge(){}
    Edge(int a , int b , int c , int d) {
        next = a , to = b , cap = c  , flow = d ;
    }
};

int head[N] ; Edge err[M] ; int nedge ;
int dis[N] , gap[N] ; int gnode ;
int super_source , super_sink , start , dest ;
int n , m ;
int need ;

void add_edge(int a , int b , int c , int d) {
    if(head[a] == -1) ++gnode ;
    if(head[b] == -1) ++gnode ;
    err[++nedge] = Edge(head[a] , b , c , d) ; head[a] = nedge ;
    err[++nedge] = Edge(head[b] , a , 0 , 0) ; head[b] = nedge ;
}

void init() {
    memset(head , -1 , sizeof(head)) ; nedge = -1 ;  gnode = 0 ;
    super_source = 0 , super_sink = n+1 ;
    int a , b , c , d ;
    int tot[N] = {0} ;
    for(int i = 0 ; i < m ; ++i) {
        scanf("%d%d%d%d" ,&a ,&b ,&c,&d) ;
        if(d) {
            add_edge(a , b , c , 0) ;
            tot[a] -= c ;
            tot[b] += c ;
        }else {
            add_edge(a , b ,  c , c) ;
        }
    }
    need = 0 ;
    for(int i = 1 ; i <= n ; ++i) {
        if(tot[i] > 0) {
            need += tot[i] ;
            add_edge(super_source , i , tot[i] , tot[i]) ;
        }else if(tot[i] < 0) {
            add_edge(i , super_sink ,-tot[i] ,  -tot[i]) ;
        }
    }
}

int dfs(int u , int limit) {
    if(u == dest) return limit ;
    int f1 = 0 , f ;
    int minh = gnode-1 ;
    for(int i = head[u] ; i != -1 ; i = err[i].next) {
        int v = err[i].to ;
        if(err[i].flow > 0) {
            if(dis[v]+1 == dis[u]) {
                f = dfs(v , min(limit , err[i].flow)) ;
                err[i].flow -= f ;
                err[i^1].flow += f ;
                limit -= f ;
                f1 += f ;
                if(dis[start] >= gnode) return f1 ;
                if(limit == 0) break ;
            }
            minh = min(minh , dis[v]) ;
        }
    }
    if(f1 == 0) {
        --gap[dis[u]] ;
        if(gap[dis[u]] == 0) dis[start] = gnode ;
        dis[u] = minh + 1 ;
        ++gap[dis[u]] ;
    }
    return f1 ;
}

int sap(int a , int b) {
    start = a , dest = b ;
    memset(dis , 0 , sizeof(dis)) ;
    memset(gap , 0 , sizeof(gap)) ;
    gap[start] = gnode ;
    int all = 0 ;
    while(dis[start] < gnode) all += dfs(start , INF) ;
    return all ;
}

int main() {
    //freopen("data.in" , "r" , stdin) ;
    while(scanf("%d%d" ,&n ,&m)==2) {
        init() ;
        int sum = sap(super_source , super_sink) ;
        if(sum == need) {
            printf("0\n") ;
        }else {
            int edst = nedge+1 ;
            add_edge(n , 1 , INF , INF) ;
            sum += sap(super_source , super_sink) ;
            if(sum != need) {printf("Impossible\n") ; continue ;}
            printf("%d\n" , err[edst].cap-err[edst].flow) ;
        }
        for(int i = 0 ; i < m ; ++i) {
            int idx = i*2 ;
            printf("%d" , err[idx].cap-err[idx].flow) ;
            if(i < m-1) printf(" ") ;
            else printf("\n") ;
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值