简单的最大流问题,一样的设源点汇点,每个单位向每个餐桌连capacity为1的边,源点向每个单位连capacity为人数的边,每个餐桌向汇点连capacity为座位数的边,跑最大流,若flow=总人数则有方案,遍历每一个单位流量为1的点即可
#include<bits/stdc++.h> using namespace std; #define lowbit(x) ((x)&(-x)) typedef long long LL; const int maxm = 1e5+5; const int INF = 0x3f3f3f3f; struct edge{ int u, v, cap, flow, nex; } edges[maxm]; int head[maxm], cur[maxm], cnt, level[1024]; void init() { memset(head, -1, sizeof(head)); } void add(int u, int v, int cap) { edges[cnt] = edge{u, v, cap, 0, head[u]}; head[u] = cnt++; } void addedge(int u, int v, int cap) { add(u, v, cap), add(v, u, 0); } void bfs(int s) { memset(level, -1, sizeof(level)); queue<int> q; level[s] = 0; q.push(s); while(!q.empty()) { int u = q.front(); q.pop(); for(int i = head[u]; i != -1; i = edges[i].nex) { edge& now = edges[i]; if(now.cap > now.flow && level[now.v] < 0) { level[now.v] = level[u] + 1; q.push(now.v); } } } } int dfs(int u, int t, int f) { if(u == t) return f; for(int& i = cur[u]; i != -1; i = edges[i].nex) { edge& now = edges[i]; if(now.cap > now.flow && level[u] < level[now.v]) { int d = dfs(now.v, t, min(f, now.cap - now.flow)); if(d > 0) { now.flow += d; edges[i^1].flow -= d; return d; } } } return 0; } int dinic(int s, int t) { int maxflow = 0; for(;;) { bfs(s); if(level[t] < 0) break; memcpy(cur, head, sizeof(head)); int f; while((f = dfs(s, t, INF)) > 0) maxflow += f; } return maxflow; } void run_case() { int n, m, cap, sum = 0; init(); cin >> m >> n; int s = 0, t = m+n+1; for(int i = 1; i <= m; ++i) { cin >> cap; sum += cap; addedge(s, i, cap); } for(int i = 1; i <= n; ++i) { cin >> cap; addedge(i+m, t, cap); } for(int i = 1; i <= m; ++i) for(int j = 1; j <= n; ++j) addedge(i, j+m, 1); int flow = dinic(s, t); if(flow != sum) { cout << 0; return; } else { cout << "1 "; for(int i = 1; i <= m; ++i) { for(int u = head[i]; u!=-1; u = edges[u].nex) { int v = edges[u].v; if(v != s && edges[u].flow) { edges[u].flow--; cout << v-m << " "; } } cout << " "; } } } int main() { ios::sync_with_stdio(false), cin.tie(0); run_case(); cout.flush(); return 0; }