非常舒适的最大流
非常显然的建图方法,然而因为数组开小卡了很长时间
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <queue>
using namespace std;
const int MAXN=2500,MAXM=100005;
int head[MAXN],dep[MAXN],maxflow,nume,s,term,n,k,m,cur[MAXN];
struct edge{
int to,nxt,cap,flow;
}e[MAXM];
void adde(int from,int to,int cap){
e[++nume].to=to;
e[nume].cap=cap;
e[nume].nxt=head[from];
head[from]=nume;
}
bool bfs(){
queue<int> q;
memset(dep,0,sizeof(dep));
q.push(s);dep[s]=1;
while(!q.empty()){
int u=q.front();q.pop();
for(int i=head[u];i;i=e[i].nxt){
int v=e[i].to;
if(!dep[v]&&e[i].flow<e[i].cap){
dep[v]=dep[u]+1;
q.push(v);
}
}
}
return dep[term];
}
int dfs(int u,int flow){
if(u==term) return flow;
int tot=0;
for(int &i=cur[u];i&&tot<flow;i=e[i].nxt){
int v=e[i].to;
if(dep[v]==dep[u]+1&&e[i].flow<e[i].cap){
if(int t=dfs(v,min(flow-tot,e[i].cap-e[i].flow))){
e[i].flow+=t;
e[((i-1)^1)+1].flow-=t;
tot+=t;
}
}
}
return tot;
}
void dinic(){
while(bfs()){
for(int i=s;i<=term;i++) cur[i]=head[i];
maxflow+=dfs(s,0x3f3f3f3f);
}
}
int main(){
cin>>k>>n;
s=0,term=n+k+1;
for(int i=1;i<=k;i++){
int t;
scanf("%d",&t);
m+=t;
adde(s,i,t);adde(i,s,0);
}
for(int i=1;i<=n;i++){
int t;
scanf("%d",&t);
for(int j=1;j<=t;j++){
int ttt;
scanf("%d",&ttt);
adde(ttt,i+k,1);adde(i+k,ttt,0);
}
adde(i+k,term,1);adde(term,i+k,0);
}
dinic();
//printf("%d %d\n",m,maxflow);
if(m!=maxflow){
printf("No Solution!\n");
}else {
for(int i=1;i<=k;i++){
printf("%d: ",i);
for(int j=head[i];j;j=e[j].nxt){
if((e[j].flow)&&e[j].to!=s) printf("%d ",e[j].to-k);
}
printf("\n");
}
}
}