「网络流 24 题」试题库
思路
建立超级源点 S S S 和超级汇点 T T T,将每一类题目拆分为入点 i n in in 和出点 o u t out out,连边 i n → o u t in \rarr out in→out,边权为 k i k_i ki,也就是所需的题目数量,同时连边 o u t → T out \rarr T out→T;并对每一道题目及其对应的类型 p i p_i pi,连边 i → i n p i i \rarr in_{p_i} i→inpi,容量为 1 1 1, S → i S \rarr i S→i 边权同样为 1 1 1
跑最大流即可,看看最大流量是否为 m m m,按照残余网络的流量来输出方案
#include<bits/stdc++.h>
#define fore(i,l,r) for(int i=(int)(l);i<(int)(r);++i)
#define fi first
#define se second
#define endl '\n'
#define ull unsigned long long
#define ALL(v) v.begin(), v.end()
#define Debug(x, ed) std::cerr << #x << " = " << x << ed;
const int INF=0x3f3f3f3f;
const long long INFLL=1e18;
typedef long long ll;
constexpr int inf = 1E9;
template<class T>
struct Dinic {
struct _Edge {
int to;
T cap;
_Edge(int to, T cap) : to(to), cap(cap) {}
};
int n; //点的数量,编号从 1 开始
std::vector<_Edge> e; //链式前向星
std::vector<std::vector<int>> g; //起到链式前向星nxt的作用
std::vector<int> cur; //当前弧优化
std::vector<int> h; //深度
Dinic() {}
Dinic(int n) {
init(n);
}
void init(int n) {
this->n = n;
e.clear();
g.assign(n + 1, {});
cur.resize(n + 1);
h.resize(n + 1);
}
bool bfs(int s, int t) { //构造分层图
h.assign(n + 1, -1);
std::queue<int> que;
h[s] = 0;
que.push(s);
while (!que.empty()) {
const int u = que.front();
que.pop();
for (int i : g[u]) {
auto [v, c] = e[i];
if (c > 0 && h[v] == -1) { //下一层有容量的邻居
h[v] = h[u] + 1;
if (v == t) {
return true;
}
que.push(v);
}
}
}
return false;
}
T dfs(int u, int t, T f) {
if (u == t) {
return f;
}
auto r = f;
for (int &i = cur[u]; i < int(g[u].size()); ++i) {
const int j = g[u][i];
auto [v, c] = e[j];
if (c > 0 && h[v] == h[u] + 1) {
auto a = dfs(v, t, std::min(r, c));
e[j].cap -= a;
e[j ^ 1].cap += a;
r -= a; //r是剩余可用流量
if (r == 0) {
return f; //如果r用完,说明f跑满了
}
}
}
return f - r; //否则f-r就是已用流量
}
void addEdge(int u, int v, T c) {
g[u].push_back(e.size()); //记录在e中的下标
e.emplace_back(v, c);
g[v].push_back(e.size()); //反向边
e.emplace_back(u, 0);
}
T flow(int s, int t) {
T ans = 0;
while (bfs(s, t)) {
cur.assign(n + 1, 0); //当前弧初始化
ans += dfs(s, t, std::numeric_limits<T>::max());
}
return ans;
}
std::vector<bool> minCut() { //最小割
std::vector<bool> c(n + 1);
for (int i = 1; i <= n; i++) {
c[i] = (h[i] != -1);
}
return c;
}
struct Edge {
int from;
int to;
T cap;
T flow;
};
std::vector<Edge> edges() {
std::vector<Edge> a;
for (int i = 0; i < e.size(); i += 2) {
Edge x;
x.from = e[i + 1].to;
x.to = e[i].to;
x.cap = e[i].cap + e[i + 1].cap;
x.flow = e[i + 1].cap;
a.push_back(x);
}
return a;
}
};
int main(){
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
std::cout.tie(nullptr);
int k, n;
std::cin >> k >> n;
int sum = 0;
int S = 2 * n + k + 3, T = S + 1;
Dinic<int> dinic(2 * n + k + 5);
fore(i, 1, k + 1){
int w;
std::cin >> w;
sum += w;
dinic.addEdge(2 * n + i, T, w);
}
fore(i, 1, n + 1){
int in = 2 * i - 1, out = 2 * i;
dinic.addEdge(S, in, INF);
dinic.addEdge(in, out, 1);
int m;
std::cin >> m;
while(m--){
int x;
std::cin >> x;
dinic.addEdge(out, 2 * n + x, 1);
}
}
if(dinic.flow(S, T) != sum){
std::cout << "No Solution!";
return 0;
}
auto edge = dinic.edges();
std::vector<std::vector<int>> ans(k + 1, std::vector<int>());
for(auto [fr, to, cap, flow] : edge){
if(fr <= 2 * n && fr % 2 == 0 && to > 2 * n && to <= 2 * n + k && flow){
int u = fr >> 1, v = to - 2 * n;
ans[v].push_back(u);
}
}
fore(i, 1, k + 1){
std::cout << i << ": ";
for(auto x : ans[i]) std::cout << x << ' ';
std::cout << endl;
}
return 0;
}