sgu176 有源汇上下界最小流
题意:有一堆点和边,1起点,n终点,某些边有可能必须满流,要求满足条件的最小流
解法:按原图建边,满流的即上下界都是容量,但是这样按有源汇上下界可行流求出来的可能不是最小流,那么我们需要开始建边的时候不要建从t到s的边,先跑一边从ss到tt的最大流,然后把该边加上再跑一次从ss到tt的最大流,那么从t到s的反向边流过的流量就是原图的最小流,为什么是这样呢,这是因为当我们第一遍跑最大流的时候,此时没有t到s的这条边,那么图中的流量会尽量按其他的边流,当我们第二次跑最大流的时候,流出来的都是第一次中已经无法流到终点的流量,此时再跑最大流时我们就尽量减少了t到s这条边上的流量,实际上可以看成延迟t到s的增流
- #include<bits/stdc++.h>
- #define fi first
- #define se second
- #define mp make_pair
- #define pb push_back
- #define pii pair<int,int>
- #define C 0.5772156649
- #define pi acos(-1.0)
- #define ll long long
- #define mod 1000000007
- #define ls l,m,rt<<1
- #define rs m+1,r,rt<<1|1
- using namespace std;
- const double g=10.0,eps=1e-;
- const int N=+,maxn=+,inf=0x3f3f3f3f;
- struct edge{
- int from,to,Next,c,low;
- }e[maxn<<];
- int cnt,head[N];
- int dis[N];
- int in[N],out[N];
- void add(int u,int v,int c,int low)
- {
- out[u]+=low;
- in[v]+=low;
- e[cnt].from=u;
- e[cnt].to=v;
- e[cnt].c=c;
- e[cnt].low=low;
- e[cnt].Next=head[u];
- head[u]=cnt++;
- e[cnt].from=v;
- e[cnt].to=u;
- e[cnt].c=;
- e[cnt].low=low;
- e[cnt].Next=head[v];
- head[v]=cnt++;
- }
- bool bfs(int s,int t)
- {
- memset(dis,-,sizeof dis);
- dis[s]=;
- queue<int>q;
- q.push(s);
- while(!q.empty())
- {
- int x=q.front();
- q.pop();
- if(x==t)return ;
- for(int i=head[x];~i;i=e[i].Next)
- {
- int te=e[i].to;
- if(dis[te]==-&&e[i].c>)
- {
- dis[te]=dis[x]+;
- q.push(te);
- }
- }
- }
- return ;
- }
- int dfs(int x,int mx,int t)
- {
- if(x==t)return mx;
- int flow=;
- for(int i=head[x];~i;i=e[i].Next)
- {
- int te=e[i].to,f;
- if(e[i].c>&&dis[te]==dis[x]+&&(f=dfs(te,min(mx-flow,e[i].c),t)))
- {
- e[i].c-=f;
- e[i^].c+=f;
- flow+=f;
- }
- }
- if(!flow)dis[x]=-;
- return flow;
- }
- int maxflow(int s,int t)
- {
- int ans=,f;
- while(bfs(s,t))
- {
- while((f=dfs(s,inf,t)))ans+=f;
- }
- return ans;
- }
- void init()
- {
- cnt=;
- memset(head,-,sizeof head);
- memset(in,,sizeof in);
- memset(out,,sizeof out);
- }
- int main()
- {
- ios::sync_with_stdio(false);
- cin.tie();
- int n,m;
- while(cin>>n>>m)
- {
- init();
- int s=,t=n;
- for(int i=;i<=m;i++)
- {
- int a,b,c,d;
- cin>>a>>b>>c>>d;
- if(d)add(a,b,,c);
- else add(a,b,c,);
- }
- int ss=n+,tt=n+;
- int sum=;
- for(int i=;i<=n;i++)
- {
- // cout<<in[i]<<" "<<out[i]<<endl;
- if(in[i]>out[i])sum+=in[i]-out[i],add(ss,i,in[i]-out[i],);
- else add(i,tt,out[i]-in[i],);
- }
- int flow=maxflow(ss,tt);
- add(t,s,inf,);
- flow+=maxflow(ss,tt);
- if(sum!=flow)cout<<"Impossible"<<endl;
- else
- {
- cout<<e[cnt-].c<<endl;
- for(int i=;i<*m;i+=)
- cout<<e[i^].c+e[i].low<<" ";
- cout<<endl;
- }
- }
- return ;
- }
- /********************
- ********************/
sgu176 有源汇上下界最小流的更多相关文章
- BZOJ_2502_清理雪道_有源汇上下界最小流
BZOJ_2502_清理雪道_有源汇上下界最小流 Description 滑雪场坐落在FJ省西北部的若干座山上. 从空中鸟瞰,滑雪场可以看作一个有向无环图,每条弧代表一个斜坡(即雪道), ...
- 【Loj117】有源汇上下界最小流(网络流)
[Loj117]有源汇上下界最小流(网络流) 题面 Loj 题解 还是模板题. #include<iostream> #include<cstdio> #include< ...
- hdu3157有源汇上下界最小流
题意:有源汇上下界最小流裸题,主要就是输入要用字符串的问题 #include<bits/stdc++.h> #define fi first #define se second #defi ...
- BZOJ 2502 清理雪道(有源汇上下界最小流)
题面 滑雪场坐落在FJ省西北部的若干座山上. 从空中鸟瞰,滑雪场可以看作一个有向无环图,每条弧代表一个斜坡(即雪道),弧的方向代表斜坡下降的方向. 你的团队负责每周定时清理雪道.你们拥有一架直升飞机, ...
- BZOJ 2502 清理雪道/ Luogu P4843 清理雪道 (有源汇上下界最小流)
题意 有一个有向无环图,求最少的路径条数覆盖所有的边 分析 有源汇上下界最小流板题,直接放代码了,不会的看dalao博客:liu_runda 有点长,讲的很好,静心看一定能看懂 CODE #inclu ...
- HDU 3157 Crazy Circuits (有源汇上下界最小流)
题意:一个电路板,上面有N个接线柱(标号1~N) 还有两个电源接线柱 + - 然后是 给出M个部件正负极的接线柱和最小电流,求一个可以让所有部件正常工作的总电流. 析:这是一个有源汇有上下界的 ...
- SGU 176 Flow construction(有源汇上下界最小流)
Description 176. Flow construction time limit per test: 1 sec. memory limit per test: 4096 KB input: ...
- HDU 3157 Crazy Circuits(有源汇上下界最小流)
HDU 3157 Crazy Circuits 题目链接 题意:一个电路板,上面有N个接线柱(标号1~N),还有两个电源接线柱 + -.给出一些线路,每一个线路有一个下限值求一个能够让全部部件正常工作 ...
- bzoj 2502 清理雪道 (有源汇上下界最小流)
2502: 清理雪道 Time Limit: 10 Sec Memory Limit: 128 MB Description 滑雪场坐落在FJ省西北部的若干座山上. 从空中鸟瞰,滑雪场 ...
随机推荐
- leetCode 64.Minimum Path Sum (最短路) 解题思路和方法
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which ...
- Hexo+yilia添加helper-live2d插件宠物动画,很好玩的哦~~
个人主页:https://www.yuehan.online 现在博客:www.wangyurui.top 安装模块: 博客根目录选择cmd命令窗口或者git bash 输入以下代码,安装插件 操作: ...
- cut命令学习
cut最基本的用法: -f 列号:提取第几列 -d 分隔符:按照指定分隔符分割列(默认是制表符tab) 测试用例:(制表符)
- Http1.0和Http1.1的主要区别
1.HTTP 1.1支持长连接(PersistentConnection)和请求的流水线(Pipelining)处理 HTTP 1.0规定浏览器与服务器只保持短暂的连接,浏览器的每次请求都需要与服务器 ...
- gearman相关笔记
gearman do: task: job只会在一个work上执行. 上面来自一个很好的ppt:http://www.docin.com/p-590223908.html 利用开源的Gearman框架 ...
- LeetCode:平衡二叉树【110】
LeetCode:平衡二叉树[110] 题目描述 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1. 示例 ...
- 8.22 ps课堂练习
真是做得超烂!以前学的快忘光了!
- HTML系列(2)基本的HTML标签(一)
本节介绍基本的HTML标签的使用实例. (1)h标签: <!DOCTYPE html> <html> <head> <title>示例2 ...
- [转]Navicat for oracle 提示 cannot load oci dll,193的解决方法 orcale 11g
Navicat for oracle 提示 cannot load oci dll,193的解决方法 内网有一台windows server 2012,安装了Navicat 11.1.8 连接or ...
- Android系统源代码的下载与编译
http://www.jianshu.com/p/aeaceda41798 目录 1.简介 2.官方同步源代码 3.镜像同步源代码 4.已有源代码更新 5.编译源代码 5.1编译Android 4.1 ...