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. Codeforces 596D Wilbur and Trees

    http://codeforces.com/contest/596/problem/D 题目大意: 有n棵树排成一排,高度都为h. 主人公要去砍树,每次等概率地随机选择没倒的树中最左边的树或者最右边的 ...

  2. Linux企业级项目实践之网络爬虫(15)——区分文本文件和二进制文件

    HTTP协议支持文本和二进制文件传输.最常见的html格式的页面即文本,图片.音乐等为二进制文件.我们要对这两类文件加以区分并分别处理. static char * BIN_SUFFIXES = &q ...

  3. C# - 创建List属性的简单方法

    不用担心List没有创建问题. private ObservableCollection<EquipmentItem> _optionalCollection; public Observ ...

  4. poj 2184 Cow Exhibition(dp之01背包变形)

    Description "Fat and docile, big and dumb, they look so stupid, they aren't much fun..." - ...

  5. MyBatis配置解析

    MyBatis配置文件解析(概要) 1.configuration:根元素 1.1 properties:定义配置外在化 1.2 settings:一些全局性的配置 1.3 typeAliases:为 ...

  6. 压位加速-poj-2443-Set Operation

    题目链接: http://poj.org/problem?id=2443 题目意思: 有n个集合(n<=1000),每个集合有m个数ai(m<=10000,1=<ai<=100 ...

  7. 基于注解的Spring MVC

    1.加入�jar 2.web.xml配置: <?xml version="1.0" encoding="UTF-8"?> <web-app v ...

  8. open(),close() 打开/关闭文件

    Open open()是一个系统调用函数,用来打开或创建一个文件,通过不同的oflag选项实现不同功能. 使用时open()函数需要包含的头文件:<sys/types.h>,<sys ...

  9. Kerberos-KDC

    Kerberos提供一种较好的解决方案,它是由MIT发明的,Kerberos建立了一个安全的.可信任的密钥分发中心(KDC, Key Distribution Center).Kerberos是一种认 ...

  10. compass安装

    修改ruby软件包的sources 国外服务器不给力,经常链接失败,换成国内淘宝的:https://ruby.taobao.org/ 先移除本有的sources gem sources --remov ...