[上下界网络流] SGU 194. Reactor Cooling
来源:互联网 发布:lumion是什么软件 编辑:程序博客网 时间:2024/05/16 12:05
194. Reactor Cooling
题意:
给一个无源汇上下界网络求一个可行流。
思路:
基本是模板题了,这个论文说的很清楚。
主要学习这种建图方法和思路。
#include<bits/stdc++.h>using namespace std;const int N = 5005;const int M = 130005;const int inf = ~0u>>2;struct eg{ int u, v, cap; //源点汇点流量 eg(){} eg(int a, int b, int c){ u = a, v = b, cap = c; }}edg[M]; //边数开大int fir[N], nex[M], ecnt, s, t;void add(int a, int b, int c){ edg[ecnt] = eg(a, b, c); nex[ecnt] = fir[a], fir[a] = ecnt++; edg[ecnt] = eg(b, a, 0); nex[ecnt] = fir[b], fir[b] = ecnt++;}int lev[N], q[M], top, tail;bool Bfs(int s, int t){ memset(lev, -1, sizeof(lev)); top = tail = 0; lev[s] = 0; q[tail++] = s; while( top < tail ){ int u = q[top++]; if( u == t ) return 1; for(int k = fir[u]; k != -1; k = nex[k]){ int v = edg[k].v; if( edg[k].cap && lev[v] == -1){ lev[v] = lev[u] + 1; q[tail++] = v; } } } return 0;}int Dfs(int s, int t, int low){ if( s == t ) return low; int a = 0, res = 0; for(int k = fir[s]; k != -1; k = nex[k]){ int v = edg[k].v; if(edg[k].cap && lev[v] == lev[s] +1 ){ a = Dfs(v, t, min(low - res, edg[k].cap) ); edg[k].cap -= a; edg[k^1].cap += a; res += a; if(res == low) return res; } } if(res == 0) lev[s] = -1; return res;}int Dinic(int s, int t){ int res = 0, minflow; while( Bfs(s, t) ){ while( minflow = Dfs(s, t, inf) ) res += minflow; } return res;}int inflow[M], outflow[M];int ans[M];int main(){ ecnt = 0; memset(fir, -1, sizeof(fir)); int n, m; scanf("%d%d", &n, &m); for(int a, b, c, d, i = 1; i <= m; ++i){ scanf("%d%d%d%d", &a, &b, &c, &d); ans[i] = c; add(a, b, d-c); inflow[b] += c; outflow[a] += c; } s = 0, t = n+1; for(int i = 1; i <= n; ++i){ int MM = inflow[i] - outflow[i]; if(MM > 0) add(s, i, MM), outflow[s] += MM; else if(MM < 0) add(i, t, -MM), inflow[t] += -MM; } int maxflow = Dinic(s, t); if(maxflow != outflow[s] || outflow[s] != inflow[t]) return 0*puts("NO"); puts("YES"); for(int i = 1; i <= m; ++i) printf("%d\n", ans[i] + edg[(i-1)<<1|1].cap);}
0 0
- [上下界网络流] SGU 194. Reactor Cooling
- SGU 194 Reactor Cooling-上下界网络流模板题
- sgu 194. Reactor Cooling (无源无汇上下界网络流)
- SGU 194. Reactor Cooling【无源汇上下界最大流】
- 【上下界网络流】ZOJ2314 Reactor Cooling
- SGU 194 Reactor Cooling 有上下界的网络流 网络流拆点
- 【无源汇上下界最大流】SGU 194 Reactor Cooling
- SGU 194 Reactor Cooling 无源汇上下界可行流
- SGU 194. Reactor Cooling (无源汇有上下界最大流
- ZOJ2314 Reactor Cooling (有上下界的网络流)
- ZOJ 2314 Reactor Cooling 无源汇上下界网络流
- zoj 2314 Reactor Cooling (有上下界的网络流)
- acdream 1211 Reactor Cooling 【上下界网络流 + 输出流量】
- zoj2314 Reactor Cooling --- 上下界可行流
- 【网络流】 SGU 194 Reactor Cooling 无源无汇上下界可行流(裸题)
- sgu 194 Reactor Cooling 无源汇有上下界最大流
- SGU 194 Reactor Cooling 无源汇带上下界可行流
- SGU - 194 Reactor Cooling(无源无汇有上下界最大流)
- 关于java中的反射机制理解!
- 2_Oracle体系结构概述
- java学习
- JAVA面试宝典9
- 客户端数据库(SQLite)
- [上下界网络流] SGU 194. Reactor Cooling
- 优秀程序员不得不知道的20个位运算技巧
- Request中getContextPath、getServletPath、getRequestURI、request.getRealPath的区别
- 库的三种加载方法 显示加载 隐示加载 延迟加载
- org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'springSecurityFilter
- Android 软引用(SoftReference)与LruCache
- 不算重零开始的java学习(二)
- 自定义RecyclerView实现下拉刷新,加载更多
- android Studio 2 引用 greendao