题意:有一堆点和边,1起点,n终点,某些边有可能必须满流,要求满足条件的最小流

解法:按原图建边,满流的即上下界都是容量,但是这样按有源汇上下界可行流求出来的可能不是最小流,那么我们需要开始建边的时候不要建从t到s的边,先跑一边从ss到tt的最大流,然后把该边加上再跑一次从ss到tt的最大流,那么从t到s的反向边流过的流量就是原图的最小流,为什么是这样呢,这是因为当我们第一遍跑最大流的时候,此时没有t到s的这条边,那么图中的流量会尽量按其他的边流,当我们第二次跑最大流的时候,流出来的都是第一次中已经无法流到终点的流量,此时再跑最大流时我们就尽量减少了t到s这条边上的流量,实际上可以看成延迟t到s的增流

  1. #include<bits/stdc++.h>
  2. #define fi first
  3. #define se second
  4. #define mp make_pair
  5. #define pb push_back
  6. #define pii pair<int,int>
  7. #define C 0.5772156649
  8. #define pi acos(-1.0)
  9. #define ll long long
  10. #define mod 1000000007
  11. #define ls l,m,rt<<1
  12. #define rs m+1,r,rt<<1|1
  13.  
  14. using namespace std;
  15.  
  16. const double g=10.0,eps=1e-;
  17. const int N=+,maxn=+,inf=0x3f3f3f3f;
  18.  
  19. struct edge{
  20. int from,to,Next,c,low;
  21. }e[maxn<<];
  22. int cnt,head[N];
  23. int dis[N];
  24. int in[N],out[N];
  25. void add(int u,int v,int c,int low)
  26. {
  27. out[u]+=low;
  28. in[v]+=low;
  29. e[cnt].from=u;
  30. e[cnt].to=v;
  31. e[cnt].c=c;
  32. e[cnt].low=low;
  33. e[cnt].Next=head[u];
  34. head[u]=cnt++;
  35. e[cnt].from=v;
  36. e[cnt].to=u;
  37. e[cnt].c=;
  38. e[cnt].low=low;
  39. e[cnt].Next=head[v];
  40. head[v]=cnt++;
  41. }
  42. bool bfs(int s,int t)
  43. {
  44. memset(dis,-,sizeof dis);
  45. dis[s]=;
  46. queue<int>q;
  47. q.push(s);
  48. while(!q.empty())
  49. {
  50. int x=q.front();
  51. q.pop();
  52. if(x==t)return ;
  53. for(int i=head[x];~i;i=e[i].Next)
  54. {
  55. int te=e[i].to;
  56. if(dis[te]==-&&e[i].c>)
  57. {
  58. dis[te]=dis[x]+;
  59. q.push(te);
  60. }
  61. }
  62. }
  63. return ;
  64. }
  65. int dfs(int x,int mx,int t)
  66. {
  67. if(x==t)return mx;
  68. int flow=;
  69. for(int i=head[x];~i;i=e[i].Next)
  70. {
  71. int te=e[i].to,f;
  72. if(e[i].c>&&dis[te]==dis[x]+&&(f=dfs(te,min(mx-flow,e[i].c),t)))
  73. {
  74. e[i].c-=f;
  75. e[i^].c+=f;
  76. flow+=f;
  77. }
  78. }
  79. if(!flow)dis[x]=-;
  80. return flow;
  81. }
  82. int maxflow(int s,int t)
  83. {
  84. int ans=,f;
  85. while(bfs(s,t))
  86. {
  87. while((f=dfs(s,inf,t)))ans+=f;
  88. }
  89. return ans;
  90. }
  91. void init()
  92. {
  93. cnt=;
  94. memset(head,-,sizeof head);
  95. memset(in,,sizeof in);
  96. memset(out,,sizeof out);
  97. }
  98. int main()
  99. {
  100. ios::sync_with_stdio(false);
  101. cin.tie();
  102. int n,m;
  103. while(cin>>n>>m)
  104. {
  105. init();
  106. int s=,t=n;
  107. for(int i=;i<=m;i++)
  108. {
  109. int a,b,c,d;
  110. cin>>a>>b>>c>>d;
  111. if(d)add(a,b,,c);
  112. else add(a,b,c,);
  113. }
  114. int ss=n+,tt=n+;
  115. int sum=;
  116. for(int i=;i<=n;i++)
  117. {
  118. // cout<<in[i]<<" "<<out[i]<<endl;
  119. if(in[i]>out[i])sum+=in[i]-out[i],add(ss,i,in[i]-out[i],);
  120. else add(i,tt,out[i]-in[i],);
  121. }
  122. int flow=maxflow(ss,tt);
  123. add(t,s,inf,);
  124. flow+=maxflow(ss,tt);
  125. if(sum!=flow)cout<<"Impossible"<<endl;
  126. else
  127. {
  128. cout<<e[cnt-].c<<endl;
  129. for(int i=;i<*m;i+=)
  130. cout<<e[i^].c+e[i].low<<" ";
  131. cout<<endl;
  132. }
  133. }
  134. return ;
  135. }
  136. /********************
  137.  
  138. ********************/

sgu176 有源汇上下界最小流的更多相关文章

  1. BZOJ_2502_清理雪道_有源汇上下界最小流

    BZOJ_2502_清理雪道_有源汇上下界最小流 Description        滑雪场坐落在FJ省西北部的若干座山上. 从空中鸟瞰,滑雪场可以看作一个有向无环图,每条弧代表一个斜坡(即雪道), ...

  2. 【Loj117】有源汇上下界最小流(网络流)

    [Loj117]有源汇上下界最小流(网络流) 题面 Loj 题解 还是模板题. #include<iostream> #include<cstdio> #include< ...

  3. hdu3157有源汇上下界最小流

    题意:有源汇上下界最小流裸题,主要就是输入要用字符串的问题 #include<bits/stdc++.h> #define fi first #define se second #defi ...

  4. BZOJ 2502 清理雪道(有源汇上下界最小流)

    题面 滑雪场坐落在FJ省西北部的若干座山上. 从空中鸟瞰,滑雪场可以看作一个有向无环图,每条弧代表一个斜坡(即雪道),弧的方向代表斜坡下降的方向. 你的团队负责每周定时清理雪道.你们拥有一架直升飞机, ...

  5. BZOJ 2502 清理雪道/ Luogu P4843 清理雪道 (有源汇上下界最小流)

    题意 有一个有向无环图,求最少的路径条数覆盖所有的边 分析 有源汇上下界最小流板题,直接放代码了,不会的看dalao博客:liu_runda 有点长,讲的很好,静心看一定能看懂 CODE #inclu ...

  6. HDU 3157 Crazy Circuits (有源汇上下界最小流)

    题意:一个电路板,上面有N个接线柱(标号1~N)   还有两个电源接线柱  +  - 然后是 给出M个部件正负极的接线柱和最小电流,求一个可以让所有部件正常工作的总电流. 析:这是一个有源汇有上下界的 ...

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

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

  8. HDU 3157 Crazy Circuits(有源汇上下界最小流)

    HDU 3157 Crazy Circuits 题目链接 题意:一个电路板,上面有N个接线柱(标号1~N),还有两个电源接线柱 + -.给出一些线路,每一个线路有一个下限值求一个能够让全部部件正常工作 ...

  9. bzoj 2502 清理雪道 (有源汇上下界最小流)

    2502: 清理雪道 Time Limit: 10 Sec  Memory Limit: 128 MB Description        滑雪场坐落在FJ省西北部的若干座山上. 从空中鸟瞰,滑雪场 ...

随机推荐

  1. 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 ...

  2. Hexo+yilia添加helper-live2d插件宠物动画,很好玩的哦~~

    个人主页:https://www.yuehan.online 现在博客:www.wangyurui.top 安装模块: 博客根目录选择cmd命令窗口或者git bash 输入以下代码,安装插件 操作: ...

  3. cut命令学习

    cut最基本的用法: -f 列号:提取第几列 -d 分隔符:按照指定分隔符分割列(默认是制表符tab) 测试用例:(制表符)

  4. Http1.0和Http1.1的主要区别

    1.HTTP 1.1支持长连接(PersistentConnection)和请求的流水线(Pipelining)处理 HTTP 1.0规定浏览器与服务器只保持短暂的连接,浏览器的每次请求都需要与服务器 ...

  5. gearman相关笔记

    gearman do: task: job只会在一个work上执行. 上面来自一个很好的ppt:http://www.docin.com/p-590223908.html 利用开源的Gearman框架 ...

  6. LeetCode:平衡二叉树【110】

    LeetCode:平衡二叉树[110] 题目描述 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1. 示例 ...

  7. 8.22 ps课堂练习

    真是做得超烂!以前学的快忘光了!

  8. HTML系列(2)基本的HTML标签(一)

        本节介绍基本的HTML标签的使用实例.     (1)h标签: <!DOCTYPE html> <html> <head> <title>示例2 ...

  9. [转]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 ...

  10. Android系统源代码的下载与编译

    http://www.jianshu.com/p/aeaceda41798 目录 1.简介 2.官方同步源代码 3.镜像同步源代码 4.已有源代码更新 5.编译源代码 5.1编译Android 4.1 ...