标签:fine front while back bfs 个人 struct pop head
题目地址:https://www.luogu.com.cn/problem/P3254
#include <iostream> #include <cstdio> #include <cstring> #include <queue> #include <vector> #define maxN 500 #define maxM 100000 #define INF 2e9 using namespace std; int N, M, S, T, Ans, Sum; struct Edge { int nxt, dis, to; } e[maxM]; int cnte = 1, head[maxN]; inline void Add(int i, int j, int k) { e[++cnte].dis = k, e[cnte].nxt = head[i], e[cnte].to = j, head[i] = cnte; e[++cnte].dis = 0, e[cnte].nxt = head[j], e[cnte].to = i, head[j] = cnte; } int dep[maxN]; bool bfs() { queue<int> Q; memset(dep, 0, sizeof(dep)); dep[S] = 1, Q.push(S); while (!Q.empty()) { int u = Q.front(); Q.pop(); for (int v, i = head[u]; i; i = e[i].nxt) { if (e[i].dis && !dep[v = e[i].to]) { dep[v] = dep[u] + 1; Q.push(v); } } } return dep[T]; } vector<int> p[maxN]; int dfs(int u, int in) { if (u == T) return in; int out = 0; for (int v, i = head[u]; i && in; i = e[i].nxt) { if (e[i].dis && dep[v = e[i].to] == dep[u] + 1) { int tmp = dfs(v, min(in, e[i].dis)); if (tmp > 0 && 1 <= u && u <= M) p[u].push_back(v - M); e[i].dis -= tmp, e[i ^ 1].dis += tmp; in -= tmp, out += tmp; } } if (!out) dep[u] = 0; return out; } int main() { cin >> M >> N; S = N + M + 1, T = S + 1; for (int i = 1, r; i <= M; ++i) { cin >> r, Sum += r; Add(S, i, r); } for (int i = M + 1, c; i <= M + N; ++i) { cin >> c, Add(i, T, c); for (int j = 1; j <= M; ++j) { Add(j, i, 1); } } while (bfs()) Ans += dfs(S, INF); if (Ans == Sum) printf("1\n"); else { printf("0\n"); return 0; } for (int u = 1; u <= M; ++u) { for (int i = head[u]; i; i = e[i].nxt) { if (!e[i].dis) printf("%d ", e[i].to - M); } printf("\n"); } return 0; }
标签:fine front while back bfs 个人 struct pop head
原文地址:https://www.cnblogs.com/blog-fgy/p/12330598.html