sgu 176 Flow construction(有源汇上下界最小流)

Flow construction

Time Limit: 500MS

 Memory Limit: 4096KB 64bit IO Format: %I64d & %I64u

[]   [Go Back]   [Status]  

Description

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
 
题意:给出一个网络,每条边有一个流量,给出每条边是否要满足该边满流。求是否存在满足各条边的流量情况的流量网络,如果存在,求出最小的从源点S到汇点T的流量。
思路:有源汇上下界最小流,做法是虚拟一个超级源S’和超级汇T',按上下界网络流的做法构图,求一次S‘到T’的最大流。然后连一条T到S的边(上界为INF,下界为0),然后再直接求一次S‘到T’的最大流。若从S‘出发的边都满流,则有可行解。最小流为T到S那条边的流量。
 
AC代码:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <queue>
#define ll long long
using namespace std;

const int maxn = 105;
const int INF = 1e9;

struct Edge{
    int u, v, cap, flow, next, id;
}et[maxn * maxn];
int low[maxn], cur[maxn], cnt[maxn], pre[maxn], eh[maxn], dis[maxn], du[maxn], ans[maxn * maxn];
int n, m, s, t, ss, tt, num;
void init(){
    memset(eh, -1, sizeof(eh));
    memset(du, 0, sizeof(du));
    s = 0, t = n + 1;
    num = 0;
}
void add(int u, int v, int cap, int flow, int id){
    Edge e = {u, v, cap, flow, eh[u], id};
    et[num] = e;
    eh[u] = num++;
}
void addedge(int u, int v, int cap, int id){
    add(u, v, cap, 0, id);
    add(v, u, 0, 0, -1);
}
int isap(int s, int t, int nv){
    int u, v, now, flow = 0;
    memset(low, 0, sizeof(low));
    memset(cnt, 0, sizeof(cnt));
    memset(dis, 0, sizeof(dis));
    for(u = 0; u <= nv; u++) cur[u] = eh[u];
    low[s] = INF, cnt[0] = nv, u = s;
    while(dis[s] < nv)
    {
        for(now = cur[u]; now != -1; now = et[now].next)
        if(et[now].cap - et[now].flow && dis[u] == dis[v = et[now].v] + 1) break;
        if(now != -1)
        {
            cur[u] = pre[v] = now;
            low[v] = min(low[u], et[now].cap - et[now].flow);
            u = v;
            if(u == t)
            {
                for(; u != s; u = et[pre[u]].u)
                {
                    et[pre[u]].flow += low[t];
                    et[pre[u]^1].flow -= low[t];
                }
                flow += low[t];
                low[s] = INF;
            }
        }
        else
        {
            if(--cnt[dis[u]] == 0) break;
            dis[u] = nv, cur[u] = eh[u];
            for(now = eh[u]; now != -1; now = et[now].next)
            if(et[now].cap - et[now].flow && dis[u] > dis[et[now].v] + 1)
            dis[u] = dis[et[now].v] + 1;
            cnt[dis[u]]++;
            if(u != s) u = et[pre[u]].u;
        }
    }
    return flow;
}
void input(){
    int a, b, z, c;
    for(int i = 0; i < m; i++)
    {
        scanf("%d%d%d%d", &a, &b, &z, &c);
        if(c)
        {
            du[a] -= z;
            du[b] += z;
            ans[i] = z;
        }
        else addedge(a, b, z, i);
    }
    for(int i = 1; i <= n; i++)
    {
        if(du[i] > 0) addedge(s, i, du[i], -1);
        else if(du[i] < 0) addedge(i, t, -du[i], -1);
    }
}
bool ok(){
    for(int i = eh[s]; i != -1; i = et[i].next)
    if(et[i].cap - et[i].flow) return false;
    return true;
}
void solve(){
    isap(s, t, t + 1);
    addedge(n, 1, INF, -1);
    isap(s, t, t + 1);
    if(!ok())
    {
        printf("Impossible\n");
        return;
    }
    for(int i = eh[n]; i != -1; i = et[i].next)
    if(et[i].v == 1)
    {
        printf("%d\n", et[i].flow);
        break;
    }
    for(int u = 1; u <= n; u++)
    for(int i = eh[u]; i != -1; i = et[i].next)
    if(et[i].id >= 0) ans[et[i].id] = et[i].flow;
    for(int i = 0; i < m - 1; i++) printf("%d ", ans[i]);
    printf("%d\n", ans[m - 1]);
}
int main()
{
    while(~scanf("%d%d", &n, &m))
    {
        init();
        input();
        solve();
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值