http://acm.sgu.ru/problem.php?contest=0&problem=176

有源汇上下界最小流,可以选择跟有源汇上下界最大流一样的建图方式,然后用二分去二分t->s这条边的流量,最小的可行流就是答案。

也可以这样:

跟无源汇最大流建图一样,然后不添加t->s的边

对ss->tt求最大流,然后添加t->s的边,求ss->tt最大流,若是满流,则t->s的流量就是最小流

PS:记录一下:SGU的ID是068720

第二种建图方式:

 #include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<iostream>
#define inf 0x7fffffff
const int maxn=;
const int maxe=maxn*maxn+;
int tot,go[maxe],first[maxn],next[maxe],flow[maxe];
int op[maxe],id[maxe],n,m,du[maxn],cnt[maxn],dis[maxn];
int dn[maxe];
int read(){
int t=,f=;char ch=getchar();
while (ch<''||ch>''){if (ch=='-') f=-;ch=getchar();}
while (''<=ch&&ch<=''){t=t*+ch-'';ch=getchar();}
return t*f;
}
void insert(int x,int y,int z){
tot++;
go[tot]=y;
next[tot]=first[x];
first[x]=tot;
flow[tot]=z;
}
void add(int x,int y,int z){
insert(x,y,z);op[tot]=tot+;
insert(y,x,);op[tot]=tot-;
}
int dfs(int x,int f,int S,int T,int nodes){
if (x==T) return f;
int mn=nodes,sum=;
for (int i=first[x];i;i=next[i]){
int pur=go[i];
if (flow[i]&&dis[pur]+==dis[x]){
int F=std::min(f-sum,flow[i]);
int save=dfs(pur,F,S,T,nodes);
flow[i]-=save;
flow[op[i]]+=save;
sum+=save;
if (f==sum||dis[S]>=nodes) return sum;
}
if (flow[i]) mn=std::min(mn,dis[pur]);
}
if (sum==){
cnt[dis[x]]--;
if (cnt[dis[x]]==) dis[S]=nodes;
else {
dis[x]=mn+;
cnt[dis[x]]++;
}
}
return sum;
}
int mxflow(int S,int T,int nodes){
int res=;
for (int i=;i<=nodes;i++) cnt[i]=dis[i]=;
while (dis[S]<nodes) res+=dfs(S,inf,S,T,nodes);
return res;
}
int main(){
int s,t;
n=read();m=read();
s=;t=n+;
for (int i=;i<=m;i++){
int u=read(),v=read(),c=read(),pd=read();
if (pd){
du[u]-=c;
du[v]+=c;
add(u,v,);
dn[i]=c;id[i]=tot;
}else{
add(u,v,c);
dn[i]=;id[i]=tot;
}
}
int sum=;
for (int i=;i<=n;i++)
if (du[i]<) add(i,t,-du[i]);
else add(s,i,du[i]),sum+=du[i];
int sum1=mxflow(s,t,t+);
add(n,,inf);
int sum2=mxflow(s,t,t+);
int sx=;
for (int i=first[s];i;i=next[i]){
if (flow[i]){
puts("Impossible");
return ;
}
}
printf("%d\n",flow[tot]);
for (int i=;i<=m;i++)
printf("%d ",flow[id[i]]+dn[i]); return ;
}

SGU176 Flow construction的更多相关文章

  1. sgu176 Flow Construction【有源汇有上下界最小流】

    同样是模板题. 首先将有源汇转换为无源汇,假设原来的源汇为st,我们加入的源汇为ST,那么我们应该从t到s连一条流量为+∞的边,使原来的st满足收支平衡,退化为普通节点. 分离必要边和其他边,从S到T ...

  2. Flow construction SGU - 176 有源汇有上下界最小流 二分法和回流法

    /** 题目:Flow construction SGU - 176 链接:https://vjudge.net/problem/SGU-176 题意: 有源汇有上下界的最小流. 给定n个点,m个管道 ...

  3. sgu Flow construction

    Flow construction 题目: 给出N个节点M条水管,要求在满足上下界的情况下.满足起点最小的流量. 算法: 这是最小流????不知道.仅仅知道用求解上下界最大流的方法就过了. 做这题收获 ...

  4. SGU 176 Flow construction(有源汇上下界最小流)

    Description 176. Flow construction time limit per test: 1 sec. memory limit per test: 4096 KB input: ...

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

    [题目链接] http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=11025 [模型] 有源汇点的上下界最小流.即既满足上下界又满足 ...

  6. SGU 176.Flow construction (有上下界的最大流)

    时间限制:0.5s 空间限制:4M 题意: 有一个由管道组成的网络,有n个节点(n不大于100),1号节点可以制造原料,最后汇集到n号节点.原料通过管道运输.其中有一些节点有管道连接,这些管道都有着最 ...

  7. SGU 176 Flow construction (有源有汇有上下界最小流)

    题意:给定 n 个点,m 条有向边,如果有向边的标号是1的话,就表示该边的上界下界都为容量 ,如果有向边的标号为0的哈,表示该边的下界为0,上界为容量 ,现在问,从 1 到 n 的最小流是多少,并输出 ...

  8. SGU 176 Flow construction【有上下界最小流】

    正好考到了所以翻一些题来做--猛然发现搞了半个月的网络流却没做两道上下界(不过这种题好像是比较少233) 首先建立超级源汇ss,tt,没限制的边照常连,对于有限制的边(u,v,mn,mx),连接(u, ...

  9. Kuangbin 带你飞专题十一 网络流题解 及模版 及上下界网络流等问题

    首先是几份模版 最大流:虽然EK很慢但是优势就是短.求最小割的时候可以根据增广时的a数组来判断哪些边是割边.然而SAP的最大流版我只会套版,并不知道该如何找到这个割边.在尝试的时候发现了一些问题.所以 ...

随机推荐

  1. FJ省队集训DAY5 T1

    思路:考试的时候打了LCT,自以为能过,没想到只能过80.. 考完一想:lct的做法点数是100W,就算是nlogn也会T. 讲一下lct的做法把:首先如果一条边连接的两个点都在同一个联通块内,那么这 ...

  2. Codeforces 479E Riding in a Lift

    http://codeforces.com/problemset/problem/432/D 题目大意: 给出一栋n层的楼,初始在a层,b层不能去,每次走的距离必须小于当前位置到b的距离,问用电梯来回 ...

  3. usaco silver

    大神们都在刷usaco,我也来水一水 1606: [Usaco2008 Dec]Hay For Sale 购买干草   裸背包 1607: [Usaco2008 Dec]Patting Heads 轻 ...

  4. Polymorphism & Overloading & Overriding

    In Java, a method signature is the method name and the number and type of its parameters. Return typ ...

  5. puppet cert maintain

  6. fdm_search_info_w_book_chain

    数据知识管理平台-元数据管理-fdm_search_info_w_book_chain-详细信息 fdm_search_info_w_book_chain

  7. Git 2.7: 一个新的带来许多新特性和性能提升的主要版本

    在2.6版本发布两个月之后,Git 2.7发布.该版本带来了许多新特性以及性能的提升. 本文选取了Git 2.7带来的主要变化: git remote支持get-url子命令,可以显示指定远端的URL ...

  8. Spark Yarn-cluster与Yarn-client

    摘要 在Spark中,有Yarn-Client和Yarn-Cluster两种模式可以运行在Yarn上,通常Yarn-cluster适用于生产环境,而Yarn-Cluster更适用于交互,调试模式,以下 ...

  9. windows系统还原

    windows系统还原 windows 系统还原有两种方法: 方法一.开始-控制面板-系统和安全-备份和还原 (或者开始—所有程序—附件—系统工具—系统还原) 详细请看下面的截图说明 方法二.开机的时 ...

  10. javascript紧接上一张for循环的问题,我自己的理解

    这类问题,通常都是在for循环里,根据i的变化作为dom的下标来作当前dom的点击事件, 预期是,每个点击事件都对应相应的i下标,但是问题是,每次点击的都是最后一次节点的dom. 原因就是其实我们作点 ...