解题报告 『酒店之王(网络最大流 + 拆点)』
Posted kirisame-marisa
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了解题报告 『酒店之王(网络最大流 + 拆点)』相关的知识,希望对你有一定的参考价值。
网络流板子题 + 拆点,个人觉得蓝题比较合适。
尽管我一开始只得了10分。
具体还是看代码吧。
代码实现如下:
#include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (register int i = a; i <= b; i++) const int inf = 0x3f3f3f3f, maxn = 1e4 + 5; int n, p, q, S, T, ans = 0, num_edge = -1; int cur[maxn], dep[maxn], head[maxn]; queue<int> Q; struct node{ int to, dis, nxt; }edge[maxn]; void origin(){memset(head, -1, sizeof(head));} int read() { int x = 0, flag = 0; char ch = ‘ ‘; while (ch != ‘-‘ && (ch < ‘0‘ || ch > ‘9‘)) ch = getchar(); if (ch == ‘-‘) { flag = 1; ch = getchar(); } while (ch >= ‘0‘ && ch <= ‘9‘) { x = (x << 1) + (x << 3) + ch - ‘0‘; ch = getchar(); } return flag ? -x : x; } void addedge(int from, int to, int dis) { edge[++num_edge].nxt = head[from]; edge[num_edge].to = to; edge[num_edge].dis = dis; head[from] = num_edge; } int bfs(int S, int T) { memset(dep, 0, sizeof(dep)); while (!Q.empty()) Q.pop(); memcpy(cur, head, sizeof(head));//一开始用的for循环,T了9个点,改成memcpy后就过了. dep[S] = 1; Q.push(S); while (!Q.empty()) { int u = Q.front(); Q.pop(); for (int i = head[u]; ~i; i = edge[i].nxt) { int v = edge[i].to; if (!dep[v] && edge[i].dis) { dep[v] = dep[u] + 1; Q.push(v); } } } if (dep[T]) return 1; return 0; } int dfs(int u, int flow) { if (u == T || !flow) return flow; int d, used = 0; for (int i = cur[u]; ~i; i = edge[i].nxt) { cur[u] = i; int v = edge[i].to; if (dep[v] == dep[u] + 1 && (d = dfs(v, min(flow, edge[i].dis)))) { used += d; flow -= d; edge[i].dis -= d; edge[i ^ 1].dis += d; if (!flow) break; } } if (!used) dep[u] = -2; return used; } int dinic() { int ans = 0; while (bfs(S, T)) ans += dfs(S, inf); return ans; } void write(int x) { if (x < 0) { putchar(‘-‘); x = -x; } if (x > 9) write(x / 10); putchar(x % 10 + ‘0‘); } int main() { origin(); n = read(), p = read(), q = read(); S = 0, T = 500; rep(i, 1, p) { addedge(S, i, 1); addedge(i, S, 0); } rep(i, 1, q) { addedge(i + p, T, 1); addedge(T, i + p, 0); } rep(i, 1, n) { addedge(i + p + q, i + p + q + n, 1); addedge(i + p + q + n, i + p + q, 0); } rep(i, 1, n) { rep(j, 1, p) { int x; x = read(); if (x == 1) { addedge(j, i + p + q, 1); addedge(i + p + q, j, 0); } } } rep(i, 1, n) { rep(j, 1, q) { int x; x = read(); if (x == 1) { addedge(i + p + q + n, j + p, 1); addedge(j + p, i + p + q + n, 0); } } } ans = dinic(); write(ans); return 0; }
以上是关于解题报告 『酒店之王(网络最大流 + 拆点)』的主要内容,如果未能解决你的问题,请参考以下文章