SGU176 Flow construction
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的更多相关文章
- sgu176 Flow Construction【有源汇有上下界最小流】
同样是模板题. 首先将有源汇转换为无源汇,假设原来的源汇为st,我们加入的源汇为ST,那么我们应该从t到s连一条流量为+∞的边,使原来的st满足收支平衡,退化为普通节点. 分离必要边和其他边,从S到T ...
- Flow construction SGU - 176 有源汇有上下界最小流 二分法和回流法
/** 题目:Flow construction SGU - 176 链接:https://vjudge.net/problem/SGU-176 题意: 有源汇有上下界的最小流. 给定n个点,m个管道 ...
- sgu Flow construction
Flow construction 题目: 给出N个节点M条水管,要求在满足上下界的情况下.满足起点最小的流量. 算法: 这是最小流????不知道.仅仅知道用求解上下界最大流的方法就过了. 做这题收获 ...
- SGU 176 Flow construction(有源汇上下界最小流)
Description 176. Flow construction time limit per test: 1 sec. memory limit per test: 4096 KB input: ...
- sgu 176 Flow construction(有源汇的上下界最小流)
[题目链接] http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=11025 [模型] 有源汇点的上下界最小流.即既满足上下界又满足 ...
- SGU 176.Flow construction (有上下界的最大流)
时间限制:0.5s 空间限制:4M 题意: 有一个由管道组成的网络,有n个节点(n不大于100),1号节点可以制造原料,最后汇集到n号节点.原料通过管道运输.其中有一些节点有管道连接,这些管道都有着最 ...
- SGU 176 Flow construction (有源有汇有上下界最小流)
题意:给定 n 个点,m 条有向边,如果有向边的标号是1的话,就表示该边的上界下界都为容量 ,如果有向边的标号为0的哈,表示该边的下界为0,上界为容量 ,现在问,从 1 到 n 的最小流是多少,并输出 ...
- SGU 176 Flow construction【有上下界最小流】
正好考到了所以翻一些题来做--猛然发现搞了半个月的网络流却没做两道上下界(不过这种题好像是比较少233) 首先建立超级源汇ss,tt,没限制的边照常连,对于有限制的边(u,v,mn,mx),连接(u, ...
- Kuangbin 带你飞专题十一 网络流题解 及模版 及上下界网络流等问题
首先是几份模版 最大流:虽然EK很慢但是优势就是短.求最小割的时候可以根据增广时的a数组来判断哪些边是割边.然而SAP的最大流版我只会套版,并不知道该如何找到这个割边.在尝试的时候发现了一些问题.所以 ...
随机推荐
- 常用的Windows批处理
切换执行路径 如果不换盘的话:cd xxx换盘:cd /d xxx 获取当前日期 编写Windows批处理时经常会需要使用到日期和时间作为文件名,所以是非常重要的. 如何获取日期呢?格式: ...
- BZOJ1108: [POI2007]天然气管道Gaz
1108: [POI2007]天然气管道Gaz Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 410 Solved: 211[Submit][Sta ...
- linux/unix 段错误捕获【续】
本文为“在C/C++中捕获段错误,打印出错的具体位置”的续篇,进一步解决涉及动态链接库的情况. 背景知识: ·linux/unix下动态链接库的基本原理 ·/proc/pid/maps文件的基本格 ...
- bzoj1626[Usaco2007 Dec]Building Roads 修建道路
Description Farmer John最近得到了一些新的农场,他想新修一些道路使得他的所有农场可以经过原有的或是新修的道路互达(也就是说,从任一个农场都可以经过一些首尾相连道路到达剩下的所有农 ...
- chapter 2: Representing and manipulating information
C allows conversion between unsigned and signed. The rule is that the underlying bit representation ...
- c++ 之 字符和字符串
字符 1.字符的分类 字符主要包括字母.数字.标点符号.控制字符等 在ASCII编码表中,每一个字符都用一个十进制数来表示 注:ASCII的全称是American Standard Code for ...
- iOS 消息推送原理
一.消息推送原理: 在实现消息推送之前先提及几个于推送相关概念,如下图: 1. Provider:就是为指定IOS设备应用程序提供Push的服务器,(如果IOS设备的应用程序是客户端的话,那么Prov ...
- Perl 多线程模块 Parallel::ForkManager
Perl 多线程模块 Parallel::ForkManager 一个简单的并行处理模块.这个是用来对付循环的多线程处理. 放在循环前面. Table of Contents 1 Synops内容简介 ...
- iOS学习之 plist文件的读写
在做iOS开发时,经经常使用到到plist文件, 那plist文件是什么呢? 它全名是:Property List,属性列表文件,它是一种用来存储串行化后的对象的文件.属性列表文件的扩展名为.pli ...
- webService返回自定义类型的数据处理
1.自定义一个Student 数据类型: package com.chnic.webservice; import java.io.Serializable; public class Student ...