zoukankan      html  css  js  c++  java
  • (一道模板题) 无源汇有上下界可行流

     
     题目描述

    这是一道模板题。

    n 个点,m  条边,每条边 e  有一个流量下界 lower(e) 和流量上界 upper(e),求一种可行方案使得在所有点满足流量平衡条件的前提下,所有边满足流量限制。

    输入格式

    第一行两个正整数 n m

    之后的 m 行,每行四个整数 s t lower upper

    输出格式

    如果无解,输出一行 NO

    否则第一行输出 YES,之后 m  行每行一个整数,表示每条边的流量。

    样例

    样例输入 1

    4 6
    1 2 1 2
    2 3 1 2
    3 4 1 2
    4 1 1 2
    1 3 1 2
    4 2 1 2

    样例输出 1

    NO

    样例输入 2

    4 6
    1 2 1 3
    2 3 1 3
    3 4 1 3
    4 1 1 3
    1 3 1 3
    4 2 1 3

    样例输出 2

    YES
    1
    2
    3
    2
    1
    1

    数据范围与提示

    N<=200,M<=10200

    分类标签

     
     
    码了一天了懒得再打一下上下界网络流原理来,,,就是一个流量平衡。
    #include<bits/stdc++.h>
    #define ll long long
    #define maxn 1005
    #define pb push_back
    using namespace std;
    const int inf=1<<29;
    vector<int> g[maxn];
    struct lines{
        int to,flow,cap;
    }l[maxn*maxn];
    int t=-1,S,T,n,m;
    int d[maxn],cur[maxn];
    bool v[maxn];
    
    inline void add(int xx,int yy,int zz){
        l[++t]=(lines){yy,0,zz},g[xx].pb(t);
        l[++t]=(lines){xx,0,0},g[yy].pb(t);
    }
    
    inline bool bfs(){
        queue<int> q;
        memset(v,0,sizeof(v));
        d[S]=0,v[S]=1,q.push(S);
        
        int x; lines e;
        while(!q.empty()){
            x=q.front(),q.pop();
            for(int i=g[x].size()-1;i>=0;i--){
                e=l[g[x][i]];
                if(!v[e.to]&&e.flow<e.cap){
                    d[e.to]=d[x]+1;
                    v[e.to]=1;
                    q.push(e.to);
                }
            }
        }
    
        return v[T];
    }
    
    int dfs(int x,int a){
        if(x==T||!a) return a;
        int flow=0,f,sz=g[x].size();
        for(int &i=cur[x];i<sz;i++){
            lines &e=l[g[x][i]];
            if(d[x]==d[e.to]-1&&(f=dfs(e.to,min(a,e.cap-e.flow)))){
                flow+=f,a-=f;
                e.flow+=f,l[g[x][i]^1].flow-=f;
                if(!a) break;
            }
        }
        
        return flow;
    }
    
    inline int max_flow(){
        int an=0;
        while(bfs()){
            memset(cur,0,sizeof(cur));
            an+=dfs(S,inf);
        }
        return an;
    }
    
    int low[maxn*50],inflow[maxn],tot=0;
    int main(){
        int uu,vv,r;
        scanf("%d%d",&n,&m);
        for(int i=0;i<m;i++){
            scanf("%d%d%d%d",&uu,&vv,low+i,&r);
            add(uu,vv,r-low[i]);
            inflow[vv]+=low[i];
            inflow[uu]-=low[i];
        }
        
        S=0,T=n+1;
        for(int i=1;i<=n;i++){
            if(inflow[i]>0){
                add(S,i,inflow[i]);
                tot+=inflow[i];
            }
            else if(inflow[i]) add(i,T,-inflow[i]);
        }
        
        if(tot!=max_flow()) puts("NO");
        else{
            puts("YES");
            for(int i=0;i<m;i++) printf("%d
    ",low[i]+l[i<<1].flow);
        }
        
        return 0;
    }
  • 相关阅读:
    为什么要做服务化?
    同步 互斥锁 读写锁 区别
    http request header 中的host行的作用
    关于group by 、group by having、where group by与 group by order by
    where,having与 group by连用的区别
    数据库:drop、truncate、delete三者删除的区别
    设置 TOMCAT 请求超时时间 和 最大连接数
    访问平台Servlet时,如何设置超时时间
    开源一款简单清爽的日历组件,JavaScript版的
    关于响应式布局
  • 原文地址:https://www.cnblogs.com/JYYHH/p/8361696.html
Copyright © 2011-2022 走看看