这是模板题了吧,先建立附加源汇,然后保留每个点的in-out,如果这个值是正的,那么就从附加源先这个点连一个边权为in-out的边,否则从这个点向附加汇连一条相反数的边,剩下题目中的边就用上界-下界连就好了。

 #include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define drep(i, a, b) for (int i = a; i >= b; i--)
#define REP(i, a, b) for (int i = a; i < b; i++)
#define mp make_pair
#define pb push_back
#define clr(x) memset(x, 0, sizeof(x))
#define xx first
#define yy second
using namespace std;
typedef long long i64;
typedef pair<int, int> pii;
const int inf = ~0U >> ;
const i64 INF = ~0ULL >> ;
//*********************************** const int maxn = , maxm = ; struct Ed {
int u, v, nx, c; Ed() {}
Ed(int _u, int _v, int _nx, int _c) :
u(_u), v(_v), nx(_nx), c(_c) {}
} E[maxm << ];
int G[maxn], edtot = ;
void addedge(int u, int v, int c) {
E[++edtot] = Ed(u, v, G[u], c);
G[u] = edtot;
E[++edtot] = Ed(v, u, G[v], );
G[v] = edtot;
} int level[maxn], s, t;
bool bfs() {
static int que[maxn]; int qh(), qt();
clr(level);
level[que[++qt] = s] = ;
while (qh != qt) {
int x = que[++qh]; if (qh == maxn - ) qh = ;
for (int i = G[x]; i; i = E[i].nx) if (E[i].c && !level[E[i].v]) {
level[que[++qt] = E[i].v] = level[x] + ;
if (qt == maxn - ) qt = ;
}
}
return !!level[t];
}
int dfs(int u, int rm) {
if (u == t) return rm;
int rm1 = rm;
for (int i = G[u]; i; i = E[i].nx) {
if (E[i].c && level[E[i].v] == level[u] + ) {
int flow = dfs(E[i].v, min(E[i].c, rm));
E[i].c -= flow, E[i ^ ].c += flow;
if ((rm -= flow) == ) break;
}
}
if (rm1 == rm) level[u] = ;
return rm1 - rm;
} int l[maxm], in[maxn], out[maxn];
int cnt;
int main() {
clr(G), edtot = , clr(in), clr(out);
int n, m;
scanf("%d%d", &n, &m);
s = n + , t = n + ;
rep(i, , m) {
int x, y, a, b; scanf("%d%d%d%d", &x, &y, &a, &b); l[i] = a;
addedge(x, y, b - a);
in[y] += a, out[x] += a;
}
int sum();
rep(i, , n) {
if (in[i] > out[i]) addedge(s, i, in[i] - out[i]), sum += in[i] - out[i];
else addedge(i, t, out[i] - in[i]);
}
int ans();
while (bfs()) ans += dfs(s, 0x3f3f3f3f);
if (ans != sum) { puts("NO"); return ; }
puts("YES");
for (int i = ; i <= m; i++) printf("%d\n", E[(i << ) ^ ].c + l[i]);
return ;
}

sgu194 Reactor Cooling【无源汇有上下界可行流】的更多相关文章

  1. ZOJ 2314 Reactor Cooling(无源汇有上下界可行流)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2314 题目大意: 给n个点,及m根pipe,每根pipe用来流躺 ...

  2. SGU 194 Reactor Cooling 无源汇带上下界可行流

    Reactor Cooling time limit per test: 0.5 sec. memory limit per test: 65536 KB input: standard output ...

  3. ZOJ 2314 (sgu 194) Reactor Cooling (无源汇有上下界最大流)

    题意: 给定n个点和m条边, 每条边有流量上下限[b,c], 求是否存在一种流动方法使得每条边流量在范围内, 而且每个点的流入 = 流出 分析: 无源汇有上下界最大流模板, 记录每个点流的 in 和 ...

  4. Zoj 2314 Reactor Cooling(无源汇有上下界可行流)

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1314 题意:    给n个点,及m根pipe,每根pipe用来流躺液体的,单向 ...

  5. LOJ [#115. 无源汇有上下界可行流](https://loj.ac/problem/115)

    #115. 无源汇有上下界可行流 先扔个板子,上下界的东西一点点搞,写在奇怪的合集里面 Code: #include <cstdio> #include <cstring> # ...

  6. 2018.08.20 loj#115. 无源汇有上下界可行流(模板)

    传送门 又get到一个新技能,好兴奋的说啊. 一道无源汇有上下界可行流的模板题. 其实这东西也不难,就是将下界变形而已. 准确来说,就是对于每个点,我们算出会从它那里强制流入与流出的流量,然后与超级源 ...

  7. [loj#115] 无源汇有上下界可行流 网络流

    #115. 无源汇有上下界可行流 内存限制:256 MiB时间限制:1000 ms标准输入输出 题目类型:传统评测方式:Special Judge 上传者: 匿名 提交提交记录统计讨论测试数据   题 ...

  8. loj#115. 无源汇有上下界可行流

    \(\color{#0066ff}{ 题目描述 }\) 这是一道模板题. \(n\) 个点,\(m\) 条边,每条边 \(e\) 有一个流量下界 \(\text{lower}(e)\) 和流量上界 \ ...

  9. 【LOJ115】无源汇有上下界可行流(模板题)

    点此看题面 大致题意: 给你每条边的流量上下界,让你判断是否存在可行流.若有,则还需输出一个合法方案. 大致思路 首先,每条边既然有一个流量下界\(lower\),我们就强制它初始流量为\(lower ...

  10. LibreOJ #115. 无源汇有上下界可行流

    二次联通门 : LibreOJ #115. 无源汇有上下界可行流 /* LibreOJ #115. 无源汇有上下界可行流 板子题 我也就会写写板子题了.. */ #include <cstdio ...

随机推荐

  1. robotframework常见问题解决汇总

    1.select window 失效 当关闭弹出框后,回到原页面,或者关闭弹出框后,又弹出新的对话框,导致select window 失效,报错 window not found 在select wi ...

  2. HDU 3861 The King’s Problem(tarjan连通图与二分图最小路径覆盖)

    题意:给我们一个图,问我们最少能把这个图分成几部分,使得每部分内的任意两点都能至少保证单向连通. 思路:使用tarjan算法求强连通分量然后进行缩点,形成一个新图,易知新图中的每个点内部的内部点都能保 ...

  3. ZOJ Problem - 2588 Burning Bridges tarjan算法求割边

    题意:求无向图的割边. 思路:tarjan算法求割边,访问到一个点,如果这个点的low值比它的dfn值大,它就是割边,直接ans++(之所以可以直接ans++,是因为他与割点不同,每条边只访问了一遍) ...

  4. P2P应用中的NAT穿透问题

    多年前曾经写过一个关于NAT钻洞的实验.现在发现那个做法在我现在的路由器上已经不管用了.经过一番搜索发现时过境迁,世界变化很快,新路由器已经是UPnP了.在这里重新理一下几种方法. 第一种,也是不太靠 ...

  5. 고 보니: 做完A发现新的事实B

    1. 선생님 얘기를 듣고 보니 이제 이해가 되네요. 2. 막상 옷을 입고 보니 꽤 잘 어우리는 것 같았다. 不能用于过去式 다 보니和 고 보니区别 다 보니: 表示在做某事过程中发现某状 ...

  6. Android----drawable state各个属性详解----ListView几个比较特别的属性:

    android:drawable 放一个drawable资源android:state_pressed 是否按下,如一个按钮触摸或者点击.android:state_focused 是否取得焦点,比如 ...

  7. UIApearance

    转载自:http://www.cocoachina.com/ios/20150723/12671.html 文章开头先援引一下Mattt Thompson大神在UIApearance里的一句话吧: 1 ...

  8. HDU 5534 Partial Tree

    2015 ACM/ICPC 长春现场赛 H题 完全背包 #include<cstdio> #include<cstring> #include<cmath> #in ...

  9. 需要注意的subList方法!和substring是不一样的!从源码解释他们的不同。

    很多时候我们截取字符串用的是substring方法,很自然用着,但是对于列表的截取时很多时候就用得很少,但是其实他们是很不一样的,具体哪里不一样呢? package main; import java ...

  10. Chapter 1 First Sight——2

    "Bella," my mom said to me — the last of a thousand times — before I got on the plane. &qu ...