题意:给出一张有向图,每次你可以从图中的任意一点出发,经过若干条边后停止,然后问你最少走几次可以将图中的每条边都走过至少一次,并且要输出方案,这个转化为网络流的话,就相当于 求一个最小流,并且存在下界,即每条边至少走一次。

析:转载:http://blog.csdn.net/sdj222555/article/details/40380423

这上面说的很清楚了,就是先求一遍是正向的,然后再求逆向的,这个逆向就是为了求可以减少的边。

代码如下:

  1. #pragma comment(linker, "/STACK:1024000000,1024000000")
  2. #include <cstdio>
  3. #include <string>
  4. #include <cstdlib>
  5. #include <cmath>
  6. #include <iostream>
  7. #include <cstring>
  8. #include <set>
  9. #include <queue>
  10. #include <algorithm>
  11. #include <vector>
  12. #include <map>
  13. #include <cctype>
  14. #include <cmath>
  15. #include <stack>
  16. #include <sstream>
  17. #include <list>
  18. #include <assert.h>
  19. #include <bitset>
  20. #include <numeric>
  21. #define debug() puts("++++")
  22. #define gcd(a, b) __gcd(a, b)
  23. #define lson l,m,rt<<1
  24. #define rson m+1,r,rt<<1|1
  25. #define fi first
  26. #define se second
  27. #define pb push_back
  28. #define sqr(x) ((x)*(x))
  29. #define ms(a,b) memset(a, b, sizeof a)
  30. #define sz size()
  31. #define pu push_up
  32. #define pd push_down
  33. #define cl clear()
  34. #define all 1,n,1
  35. #define FOR(i,x,n) for(int i = (x); i < (n); ++i)
  36. #define freopenr freopen("in.txt", "r", stdin)
  37. #define freopenw freopen("out.txt", "w", stdout)
  38. using namespace std;
  39.  
  40. typedef long long LL;
  41. typedef unsigned long long ULL;
  42. typedef pair<int, int> P;
  43. const int INF = 0x3f3f3f3f;
  44. const LL LNF = 1e17;
  45. const double inf = 1e20;
  46. const double PI = acos(-1.0);
  47. const double eps = 1e-3;
  48. const int maxn = 100 + 40;
  49. const int maxm = (100000 << 8) + 10;
  50. const int mod = 1000000007;
  51. const int dr[] = {-1, 0, 1, 0};
  52. const int dc[] = {0, -1, 0, 1};
  53. const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
  54. int n, m;
  55. const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  56. const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  57. inline bool is_in(int r, int c) {
  58. return r >= 0 && r < n && c >= 0 && c < m;
  59. }
  60.  
  61. struct Edge{
  62. int from, to, cap, flow;
  63. };
  64.  
  65. struct Dinic{
  66. int n, m, s, t;
  67. vector<Edge> edges;
  68. vector<int> G[maxn];
  69. int d[maxn];
  70. bool vis[maxn];
  71. int cur[maxn];
  72.  
  73. void init(int n){
  74. this-> n = n;
  75. FOR(i, 0, n) G[i].cl;
  76. edges.cl;
  77. }
  78.  
  79. void addEdge(int from, int to, int cap){
  80. edges.pb((Edge){from, to, cap, 0});
  81. edges.pb((Edge){to, from, 0, 0});
  82. m = edges.sz;
  83. G[from].pb(m - 2);
  84. G[to].pb(m - 1);
  85. }
  86.  
  87. bool bfs(){
  88. ms(vis, 0); vis[s] = 1;
  89. d[s] = 0;
  90. queue<int> q; q.push(s);
  91.  
  92. while(!q.empty()){
  93. int u = q.front(); q.pop();
  94. for(int i = 0; i < G[u].sz; ++i){
  95. Edge &e = edges[G[u][i]];
  96. if(!vis[e.to] && e.cap > e.flow){
  97. d[e.to] = d[u] + 1;
  98. vis[e.to] = 1;
  99. q.push(e.to);
  100. }
  101. }
  102. }
  103. return vis[t];
  104. }
  105.  
  106. int dfs(int u, int a){
  107. if(u == t || a == 0) return a;
  108. int flow = 0, f;
  109. for(int &i = cur[u]; i < G[u].sz; ++i){
  110. Edge &e = edges[G[u][i]];
  111. if(d[e.to] == d[u] + 1 && (f = dfs(e.to, min(a, e.cap - e.flow))) > 0){
  112. e.flow += f;
  113. edges[G[u][i]^1].flow -= f;
  114. flow += f;
  115. a -= f;
  116. if(a == 0) break;
  117. }
  118. }
  119. return flow;
  120. }
  121.  
  122. int maxflow(int s, int t){
  123. this-> s = s;
  124. this-> t = t;
  125. int flow = 0;
  126. while(bfs()){ ms(cur, 0); flow += dfs(s, INF); }
  127. return flow;
  128. }
  129. };
  130.  
  131. Dinic dinic;
  132.  
  133. int in[maxn];
  134. vector<P> edges[maxn];
  135. int main(){
  136. while(scanf("%d", &n) == 1){
  137. ms(in, 0);
  138. int s = 0, t = n + 1;
  139. dinic.init(t + 4);
  140. int ans = 0;
  141. for(int i = 1; i <= n; ++i){
  142. int x; scanf("%d", &x);
  143. while(x--){
  144. int y; scanf("%d", &y);
  145. ++in[y]; --in[i];
  146. dinic.addEdge(i, y, INF);
  147. }
  148. edges[i].cl;
  149. }
  150. int sum = 0;
  151. for(int i = 1; i <= n; ++i)
  152. if(in[i] > 0) dinic.addEdge(s, i, in[i]), ans += in[i];
  153. else dinic.addEdge(i, t, -in[i]);
  154. printf("%d\n", ans -= dinic.maxflow(s, t));
  155. for(int i = 1; i <= n; ++i){
  156. for(int j = 0; j < dinic.G[i].sz; ++j){
  157. Edge &e = dinic.edges[dinic.G[i][j]];
  158. if(e.from != i || e.to == t || e.cap == 0) continue;
  159. edges[i].pb(P(e.to, e.flow + 1));
  160. in[e.to] += e.flow;
  161. }
  162. }
  163. for(int i = 1; i <= n; ++i) while(in[i] < 0){
  164. ++in[i];
  165. vector<int> ans; ans.push_back(i);
  166. int u = i;
  167. while(1){
  168. bool ok = true;
  169. for(int j = 0; j < edges[u].sz; ++j){
  170. if(edges[u][j].se == 0) continue;
  171. ok = false;
  172. --edges[u][j].se;
  173. u = edges[u][j].fi;
  174. ans.push_back(u);
  175. break;
  176. }
  177. if(ok) break;
  178. }
  179. for(auto &it : ans) printf("%d%c", it, " \n"[it == ans.back()]);
  180. }
  181. }
  182. return 0;
  183. }

  

UVaLive 4597 Inspection (网络流,最小流)的更多相关文章

  1. 【BZOJ2502】清理雪道 有上下界的网络流 最小流

    [BZOJ2502]清理雪道 Description        滑雪场坐落在FJ省西北部的若干座山上. 从空中鸟瞰,滑雪场可以看作一个有向无环图,每条弧代表一个斜坡(即雪道),弧的方向代表斜坡下降 ...

  2. 【bzoj2502】清理雪道 有上下界最小流

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

  3. 【bzoj1458】士兵占领 有上下界最小流

    题目描述 有一个M * N的棋盘,有的格子是障碍.现在你要选择一些格子来放置一些士兵,一个格子里最多可以放置一个士兵,障碍格里不能放置士兵.我们称这些士兵占领了整个棋盘当满足第i行至少放置了Li个士兵 ...

  4. 【bzoj4200】[Noi2015]小园丁与老司机 STL-map+dp+有上下界最小流

    题目描述 小园丁 Mr. S 负责看管一片田野,田野可以看作一个二维平面.田野上有 nn 棵许愿树,编号 1,2,3,…,n1,2,3,…,n,每棵树可以看作平面上的一个点,其中第 ii 棵树 (1≤ ...

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

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

  6. 【bzoj2625】[Neerc2009]Inspection 有上下界最小流

    题目描述 You are in charge of a team that inspects a new ski resort. A ski resort is situated on several ...

  7. Bzoj 2502: 清理雪道 有上下界网络流_最小流

    好长时间没有写网络流了,感觉好手生.对于本题,设一个源点 $s$ 和 $t$.1.由 $s$ 向每个点连一条没有下界,容量为无限大的边,表示以该点为起点.2.由每个点向 $t$ 连一条没有下界,容量为 ...

  8. 【BZOJ-2502】清理雪道 有上下界的网络流(有下界的最小流)

    2502: 清理雪道 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 594  Solved: 318[Submit][Status][Discuss] ...

  9. LOJ117 有源汇有上下界最小流(上下界网络流)

    跑出可行流后从原来的汇点向原来的源点跑最大流,原图最小流=inf-maxflow.显然超源超汇的相关边对其也没有影响.原图最小流=可行流-原图新增流量,因为t向s流量增加相当于s向t流量减少.但为什么 ...

随机推荐

  1. ztree参考

    ztree一 ztree二 ztree三

  2. c++ vector, 迭代器

    现代c++尽量使用vector(容器)和迭代器(相当于指针),少使用数组和指针,除非对程序执行效率有很高的要求. 容器优点,易于扩展,可通过push_back方法动态添加元素,数组不能动态添加元素. ...

  3. JAVA学习(七)__Spring的@Autowired注入规则

    @Autowired 默认是按照byType进行注入的,但是当byType方式找到了多个符合的bean,又是怎么处理的? 经过一些代码的测试,我发现,Autowired默认先按byType,如果发现找 ...

  4. nodejs文件操作笔记

    nodejs添加了流的概念,通过流操作文件如行云流水,比早前便利畅快多了. 先来第一个例子,我们建一个stream.js文件,里面内容如下: var fs = require("fs&quo ...

  5. jquery clone 获取文本框值得问题

    1 clone 出来的文本框 默认不会把原来的事件也带过去 如果使用 $("#").clone(true);   true  可以将原来的事件带过去 获取文本框的值 可以使用事件 ...

  6. LINUX 修复relocation error: /lib/tls/libc.so.6

    错误信息: relocation error: /lib/tls/libc.so.6: symbol _dl_tls_get_addr_soft, version GLIBC_PRIVATE not ...

  7. Python合并列表,append()、extend()、+、+=

    在实际应用中涉及到了列表合并的问题. 在应用append()时,发现列表是以一个元素的形式追加到列表上的,最后查询后用的是extend()方法,下面是区别   1.append()  向列表尾部追加一 ...

  8. 删除Eclipse已有的SVN资源库位置

    点击Eclipse菜单栏的[Window]->[Show View]->[Other],在弹出的对话框中,选择[SVN]->[SVN资源库],然后点击[OK]. 接着,在Eclips ...

  9. centos 系统下查看时间时区以及修改

    1.系统时间查看及修改: Centos 6 查看系统时间:# date

  10. keras—多层感知器MLP—MNIST手写数字识别

    一.手写数字识别 现在就来说说如何使用神经网络实现手写数字识别. 在这里我使用mind manager工具绘制了要实现手写数字识别需要的模块以及模块的功能:  其中隐含层节点数量(即神经细胞数量)计算 ...