标签:数据 include bre class 强制 git dfs sdi 限制
这是一道模板题。
\(n\) 个点,\(m\) 条边,每条边 \(e\) 有一个流量下界 \(\text{lower}(e)\) 和流量上界 \(\text{upper}(e)\),求一种可行方案使得在所有点满足流量平衡条件的前提下,所有边满足流量限制。
第一行两个正整数 \(n\)、\(m\)。
之后的 \(m\) 行,每行四个整数 \(s\)、\(t\)、\(\text{lower}\)、\(\text{upper}\)。
如果无解,输出一行 NO
。
否则第一行输出 YES
,之后 \(m\) 行每行一个整数,表示每条边的流量。
4 6
1 2 1 2
2 3 1 2
3 4 1 2
4 1 1 2
1 3 1 2
4 2 1 2
4 6
1 2 1 3
2 3 1 3
3 4 1 3
4 1 1 3
1 3 1 3
4 2 1 3
NO
YES
1
2
3
2
1
1
1≤n≤200,1≤m≤10200
#include<bits/stdc++.h>
#define LL long long
LL in() {
char ch; LL x = 0, f = 1;
while(!isdigit(ch = getchar()))(ch == '-') && (f = -f);
for(x = ch ^ 48; isdigit(ch = getchar()); x = (x << 1) + (x << 3) + (ch ^ 48));
return x * f;
}
const int maxn = 3e4 + 10;
struct node {
int to, dis, id;
node *nxt, *rev;
node(int to = 0, int dis = 0, int id = 0, node *nxt = NULL, node *rev = NULL)
: to(to), dis(dis), id(id), nxt(nxt), rev(rev) {}
void *operator new(size_t) {
static node *S = NULL, *T = NULL;
return (S == T) && (T = (S = new node[1024]) + 1024), S++;
}
}*head[maxn], *cur[maxn];
int n, m, s, t, dep[maxn], ans[maxn], d[maxn];
void add(int from, int to, int c, int id) {
head[from] = new node(to, c, id, head[from], NULL);
}
void link(int from, int to, int c, int id) {
add(from, to, c, 0);
add(to, from, 0, id);
head[from]->rev = head[to];
head[to]->rev = head[from];
}
bool bfs() {
std::queue<int> q;
for(int i = s; i <= t; i++) dep[i] = 0, cur[i] = head[i];
dep[s] = 1;
q.push(s);
while(!q.empty()) {
int tp = q.front(); q.pop();
for(node *i = head[tp]; i; i = i->nxt)
if(!dep[i->to] && i->dis)
dep[i->to] = dep[tp] + 1, q.push(i->to);
}
return dep[t];
}
int dfs(int x, int change) {
if(x == t || !change) return change;
int flow = 0, ls;
for(node *i = cur[x]; i; i = i->nxt) {
cur[x] = i;
if(dep[i->to] == dep[x] + 1 && (ls = dfs(i->to, std::min(change, i->dis)))) {
flow += ls;
change -= ls;
i->dis -= ls;
i->rev->dis += ls;
if(!change) break;
}
}
return flow;
}
int dinic() {
int flow = 0;
while(bfs()) flow += dfs(s, 0x7ffffff);
return flow;
}
int main() {
n = in(), m = in();
s = 0, t = n + 1;
int x, y, l, r, tot = 0;
for(int i = 1; i <= m; i++) {
x = in(), y = in(), l = in(), r = in();
d[i] = l;
link(x, y, r - l, i);
link(s, y, l, 0);
link(x, t, l, 0);
tot += l;
}
if(tot == dinic()) {
for(int i = 1; i <= n; i++)
for(node *j = head[i]; j; j = j->nxt)
if(j->id)
ans[j->id] = d[j->id] + j->dis;
printf("YES\n");
for(int i = 1; i <= m; i++) printf("%d\n", ans[i]);
}
else printf("NO");
return 0;
}
标签:数据 include bre class 强制 git dfs sdi 限制
原文地址:https://www.cnblogs.com/olinr/p/10335831.html