当前位置: 代码迷 >> 综合 >> luogu P3254 圆桌问题(网络流24题 带权二分图匹配)
  详细解决方案

luogu P3254 圆桌问题(网络流24题 带权二分图匹配)

热度:38   发布时间:2024-02-25 03:58:58.0

题目描述

有来自 mm 个不同单位的代表参加一次国际会议。第 ii 个单位派出了 r_iri? 个代表。

会议的餐厅共有 nn 张餐桌,第 ii 张餐桌可容纳 c_ici? 个代表就餐。

为了使代表们充分交流,希望从同一个单位来的代表不在同一个餐桌就餐。请给出一个满足要求的代表就餐方案。

输入格式

输入的第一行是用空格隔开的两个整数,分别代表单位的个数 mm 和餐桌的个数 nn。
第二行有 mm 个用空格隔开的整数,第 ii 个整数代表第 ii 个单位的代表人数 r_iri?。
第三行有 nn 个用空格隔开的整数,第 ii 个整数代表第 ii 张餐桌能容纳的人数 c_ici?。

输出格式

本题存在 Special Judge
请输出是否存在满足要求的就餐方案,若存在,请给出任意一种可行的方案。
输出的第一行是一个非 00 即 11 的整数,若存在方案则输出 11,否则输出 00。
若存在方案,则对于第 22 到第 (m + 1)(m+1) 行,在第 (i + 1)(i+1) 行输出 r_iri? 个整数,代表第 ii 个单位的代表就餐的餐桌编号。

输入输出样例

输入 

4 5
4 5 3 5
3 5 2 6 4

输出

1
1 2 4 5
1 2 3 4 5
2 4 5
1 2 3 4 5

说明/提示

【数据规模与约定】

  • 对于 100\%100% 的数据,保证 1 \leq m \leq 1501≤m≤150,1 \leq n \leq 2701≤n≤270,1 \leq r_i, c_i \leq 10^31≤ri?,ci?≤103。

【提示】

  • 请注意输入的第一行先读入 mm 再读入 nn。

思路:

源点向工作单位连边,流量为r[i],圆桌向汇点连边,流量为c[i],每个单位向每个圆桌连边,流量为1。dinic跑最大流,如果最大流小于总人数说明没有方案。注意边的输出。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll N = 1e3 + 5;
const ll M = 1e5 + 5;    //边集二倍
const ll mod = 1e9 + 7;
const ll inf = 0x3f3f3f3f;ll head[N], tot, n, m, s, t, r[N];
ll to[N][N], cnt[N];struct Edge {ll from, to, next, cap, flow;
}edge[M];void init() {tot = 2;memset(head, -1, sizeof(head));
}void addedge(ll u, ll v, ll w, ll rw = 0) {edge[tot].from = u;edge[tot].to = v;edge[tot].cap = w;edge[tot].flow = 0;edge[tot].next = head[u];head[u] = tot++;edge[tot].from = v;edge[tot].to = u;edge[tot].cap = rw;edge[tot].flow = 0;edge[tot].next = head[v];head[v] = tot++;
}ll Q[N];
ll dep[N], cur[N], sta[N]; ///数组cur记录点u之前循环到了哪一条边
bool bfs(ll s, ll t) {ll fron = 0, tail = 0;memset(dep, -1, sizeof(dep));dep[s] = 0;Q[tail++] = s;while(fron < tail) {ll u = Q[fron++];for(ll i = head[u]; i != -1; i = edge[i].next) {ll v = edge[i].to;if(edge[i].cap > edge[i].flow && dep[v] == -1) {dep[v] = dep[u] + 1;if(v == t) return true;Q[tail++] = v;}}}return false;
}ll dinic(ll s, ll t) {ll maxflow = 0;while(bfs(s, t)) {for(ll i = 0; i < N; ++i) cur[i] = head[i];ll u = s, tail = 0;while(cur[s] != -1) {if(u == t) {ll tp = inf;for(ll i = tail - 1; i >= 0; --i)tp = min(tp, edge[sta[i]].cap - edge[sta[i]].flow);maxflow += tp;for(ll i = tail - 1; i >= 0; --i) {edge[sta[i]].flow += tp;edge[sta[i] ^ 1].flow -= tp;if(edge[sta[i]].cap - edge[sta[i]].flow == 0)tail = i;}u = edge[sta[tail] ^ 1].to;}else if(cur[u] != -1 && edge[cur[u]].cap > edge[cur[u]].flow && dep[u] + 1 == dep[edge[cur[u]].to]) {sta[tail++] = cur[u];u = edge[cur[u]].to;}else {while(u != s && cur[u] == -1)u = edge[sta[--tail] ^ 1].to;cur[u] = edge[cur[u]].next;}}}return maxflow;
}int main() {init();ll c;ll sum = 0;scanf("%lld%lld", &m, &n);s = 0, t = m + n + 1;for(ll i = 1; i <= m; ++i) {scanf("%lld", &r[i]);addedge(s, i, r[i]);sum += r[i];}for(ll i = 1; i <= n; ++i) {scanf("%lld", &c);addedge(i + m, t, c);}for(ll i = 1; i <= m; ++i) {for(ll j = 1; j <= n; ++j) {addedge(i, j + m, 1);}}ll maxflow = dinic(s, t);if(maxflow < sum) {printf("0\n");return 0;}printf("1\n");memset(to, 0, sizeof(to));memset(cnt, 0, sizeof(cnt));for(ll i = 0; i < tot; i += 2) {ll u = edge[i].from, v = edge[i].to;if(u == s || v == t) continue;if(edge[i].flow) {if(u > m) u -= m;if(v > m) v -= m;to[u][++cnt[u]] = v;}}for(ll i = 1; i <= m; ++i) {for(ll j = 1; j <= r[i]; ++j) {if(j > 1) printf(" ");printf("%lld", to[i][j]);}printf("\n");}return 0;
}