5. Round table questions
Title Description
Suppose there is an international conference attended by representatives from m different units. The representative number of each unit is ri(i=1,2 ·· m). There are n tables in the conference restaurant, each table can accommodate ci(i=1,2 ·· n) representatives. In order to make the representatives fully communicate, it is hoped that the representatives from the same unit will not eat at the same table. Try to design an algorithm and give a representative meal plan that meets the requirements
Input Description
The first line has two positive integers m and N, m for the number of units, n for the number of tables, 1 < = m < = 150, 1 < = n < = 270. The second line has m positive integers, representing the number of representatives of each unit. There are n positive integers in line 3 of the document, which respectively represent the capacity of each dining table
Output Description
It will represent the dining plan output. If the problem is solved, output 1 in line 1, otherwise output 0. The next m line gives the table number for each unit. If there are multiple schemes that meet the requirements, only one scheme is output
Sample Input
4 5
4 5 3 5
3 5 2 6 4
Sample Output
1
1 2 4 5
1 2 3 4 5
2 4 5
1 2 3 4 5
Analysis: obviously, we can connect each unit and each table with an edge with a capacity of 1 (only one person can be dispatched from each unit at most) to meet the requirements of different tables in the same unit
Construction drawing: s connects one side with capacity of one person to each unit, each unit connects one side with capacity of one person to each table, and each table connects one side with capacity of one person to each table
Code:
#include <bits/stdc++.h>
using namespace std;
struct node {
int to;
int next;
int flow;
}e[500*500];
int n,m,tot=1,s,t;
int d[1000],f[1000];
int head[1000],cur[1000];
bool sum[200][300];
int read() {
int ans=0,flag=1;
char ch=getchar();
while( (ch>'9' || ch<'0') && ch!='-' ) ch=getchar();
if(ch=='-') flag=-1,ch=getchar();
while(ch>='0' && ch<='9') ans=ans*10+ch-'0',ch=getchar();
return ans*flag;
}
void addedge(int u,int v,int w) {
e[++tot].to=v;
e[tot].flow=w;
e[tot].next=head[u];
head[u]=tot;
e[++tot].to=u;
e[tot].flow=0;
e[tot].next=head[v];
head[v]=tot;
return ;
}
int dfs(int now,int flow) {
if(now==t)
return flow;
int use=0;
for(int i=cur[now];i;i=e[i].next) {
cur[now]=i;
if(e[i].flow>0 && d[e[i].to]+1==d[now]) {
int temp=dfs(e[i].to,min(flow-use,e[i].flow));
use+=temp;
e[i].flow-=temp;
e[i^1].flow+=temp;
if(flow==use)
return use;
}
}
cur[now]=head[now];
if(!(--f[d[now]]))
d[s]=m+n+2;
++f[++d[now]];
return use;
}
int main() {
n=read(),m=read();
s=0,t=n+m+1;
int ans=0;
for(int i=1;i<=n;i++) {
int w=read();
ans+=w;
addedge(s,i,w);
}
for(int i=n+1;i<=n+m;i++) {
int w=read();
addedge(i,t,w);
}
for(int i=1;i<=n;i++)
for(int j=n+1;j<=n+m;j++)
addedge(i,j,1);
f[0]=n+m+2;
while(d[s]<n+m+2)
ans-=dfs(s,1<<30);
if(!ans) {
printf("1\n");
for(int i=2;i<=tot;i+=2) {
if(e[i].to==s || e[i^1].to==s) continue;
if(e[i].to==t || e[i^1].to==t) continue;
if(!e[i].flow)
sum[e[i^1].to][e[i].to-n]=true;
}
for(int i=1;i<=n;i++) {
for(int j=1;j<=m;j++)
if(sum[i][j])
printf("%d ",j);
printf("\n");
}
}
else
printf("0");
return 0;
}