[网络流24题] P3254 圆桌问题
作者:互联网
普通最大流,需要输出方案
https://www.luogu.com.cn/problem/P3254
算是很明显的建图了
点击查看代码
#include <bits/stdc++.h>
#define endl '\n'
#define IOS \
ios::sync_with_stdio(0); \
cin.tie(0); \
cout.tie(0)
#define P pair<int, int>
typedef long long ll;
using namespace std;
const int N = 150 + 270 + 5;
const int M = N * N;
const int INF = 0x3f3f3f3f;
struct edge {
int v, w, to;
} e[M * 2];
int pre[N], cnt_edge, dep[N];
int S, T, z, head[N], sum;
int n, m, q[N], cur[N];
void add(int u, int v, int w) {
// cout << "add: " << u << " " << v << endl;
e[cnt_edge] = {v, w, head[u]};
head[u] = cnt_edge++;
e[cnt_edge] = {u, 0, head[v]};
head[v] = cnt_edge++;
}
bool bfs() {
for (int i = 0; i <= T; i++) dep[i] = 0;
dep[S] = 1;
int l = 0, r = 1;
q[r] = S;
while (l < r) {
int u = q[++l];
for (int i = head[u]; i != -1; i = e[i].to) {
int v = e[i].v;
if (!dep[v] && e[i].w) dep[v] = dep[u] + 1, q[++r] = v;
}
}
return dep[T];
}
int dfs(int u, int mi) {
int res = 0;
if (mi == 0 || u == T) return mi;
for (int &i = cur[u]; i != -1; i = e[i].to) {
int v = e[i].v;
if (dep[u] + 1 == dep[v] && e[i].w) {
int minn = dfs(v, min(mi - res, e[i].w));
e[i].w -= minn;
e[i ^ 1].w += minn;
res += minn;
if (res == mi) return res;
}
}
if (res == 0) dep[u] = 0;
return res;
}
int dinic() {
ll res = 0;
while (bfs()) {
memcpy(cur, head, sizeof(head));
// cout<<res<<endl;
res += dfs(S, INF);
}
// cout << res << endl;
return res;
}
int c[150 + 5], vis[150 + 5];
int id(int x, int day) { return x + day * n; }
int main() {
memset(head, -1, sizeof head);
cin >> n >> m;
S = n + m + 1, T = S + 1;
int sum = 0;
for (int i = 1; i <= n; i++) {
cin >> c[i];
sum += c[i];
add(S, id(i, 0), c[i]);
for (int j = 1; j <= m; j++) {
add(id(i, 0), id(j, 1), 1);
}
}
for (int i = 1, c; i <= m; i++) {
cin >> c;
add(id(i, 1), T, c);
}
int ans = dinic() == sum ? 1 : 0;
cout << ans << endl;
if(ans){
for (int i = 1; i <= n; i++) {
for (int j = head[id(i, 0)]; ~j; j = e[j].to) {
if(e[j^1].w != 0){
cout << e[j].v -n << " ";
e[j].w += 1;
e[j ^ 1].w -= 1;
}
}
cout << endl;
}
}
return 0;
}
标签:24,const,cout,int,sum,add,圆桌,P3254,define 来源: https://www.cnblogs.com/FushimiYuki/p/15572136.html