https://www.luogu.org/problemnew/show/P3254
思路:这道题很水,s向每个单位连弧,容量为单位的人数,每个餐桌向t连弧,容量为餐桌容量,每个单位向每个餐桌连容量为1的弧,跑个最大流就好了。
#include <bits/stdc++.h>
using namespace std;
const int maxn=1005;
const int INF=0x3f3f3f3f;
struct Edge{
int from,to,cap,flow;
};
struct Dinic{
int n1,n2,m,s,t,sum;
vector<Edge> edges;
vector<int> G[maxn];
bool vis[maxn];
int d[maxn];
int cur[maxn];
void init()
{
int x;
cin>>n1>>n2;
s=0;t=n1+n2+1;
for(int i=1;i<=n1;i++)
{
scanf("%d",&x);
sum+=x;
AddEdge(s,i,x);
for(int j=n1+1;j<=n1+n2;j++)AddEdge(i,j,1);
}
for(int i=n1+1;i<=n1+n2;i++)
{
scanf("%d",&x);
AddEdge(i,t,x);
}
}
void AddEdge(int f,int t,int c)
{
edges.push_back((Edge){f,t,c,0});
edges.push_back((Edge){t,f,0,0});
m=edges.size();
G[f].push_back(m-2);
G[t].push_back(m-1);
}
bool bfs()
{
memset(vis,0,sizeof(vis));
queue<int> Q;
Q.push(s);
d[s]=0;
vis[s]=1;
while(!Q.empty())
{
int x=Q.front();Q.pop();
for(int i=0;i<G[x].size();i++)
{
Edge& e=edges[G[x][i]];
if(!vis[e.to] && e.cap>e.flow)
{
vis[e.to]=1;
d[e.to]=d[x]+1;
Q.push(e.to);
}
}
}
return vis[t];
}
int dfs(int x,int a)
{
if(x==t || a==0)return a;
int flow=0,f;
for(int& i=cur[x];i<G[x].size();i++)
{
Edge& e=edges[G[x][i]];
if(d[x]+1==d[e.to] && (f=dfs(e.to,min(a,e.cap-e.flow)))>0)
{
e.flow+=f;
edges[G[x][i]^1].flow-=f;
flow+=f;
a-=f;
if(!a)break;
}
}
return flow;
}
int MaxFlow()
{
int flow=0;
while(bfs())
{
memset(cur,0,sizeof(cur));
flow+=dfs(s,INF);
}
return flow;
}
void print(int flow)
{
printf("%d\n",flow==sum);
if(flow!=sum)return;
int now=1;
for(int i=0;i<m;i+=2)
{
int from=edges[i].from,to=edges[i].to;
if(from==s||from==t||to==s||to==t)continue;
if(from!=now)now++,putchar('\n');
if(edges[i].flow==1)printf("%d ",to-n1);
}
}
}ans;
int main()
{
//freopen("input.in","r",stdin);
ans.init();
int flow=ans.MaxFlow();
ans.print(flow);
return 0;
}