很经典的一道网络流了
相信网上的题解也很多很多
这里的重点放在两张图上
是上图还是下图
就会明白这个题到底什么意思的
点权和边权到底如何解释
//#include<bits/stdc++.h>
#include<stdio.h>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string.h>
using namespace std;
const int maxn=11000;
const int maxm=120000;
const int INF=0x3f3f3f3f;
struct Edge{
int to,nxt,cap,flow;
}edge[maxm];
int tol,Head[maxn];
void init(){
memset(Head,-1,sizeof(Head));
tol=2;
}
void addedge(int u,int v,int w,int rw=0){
edge[tol].to=v;
edge[tol].cap=w;
edge[tol].flow=0;
edge[tol].nxt=Head[u];
Head[u]=tol++;
edge[tol].to=u;
edge[tol].cap=rw;
edge[tol].flow=0;
edge[tol].nxt=Head[v];
Head[v]=tol++;
}
int Q[maxn],dep[maxn],cur[maxn],sta[maxn];
bool bfs(int s,int t,int n){
int Front=0,Tail=0;
memset(dep,-1,sizeof(dep[0])*(n+1));
dep[s]=0;
Q[Tail++]=s;
while(Front<Tail){
int u=Q[Front++];
for(int i=Head[u];i!=-1;i=edge[i].nxt){
int 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;
}
int dinic(int s,int t,int n){
int maxflow=0;
while(bfs(s,t,n)){
for(int i=0;i<n;i++) cur[i]=Head[i];
int u=s,tail=0;
while(cur[s]!=-1){
if (u==t) {
int tp=INF;
for(int i=tail-1;i>=0;i--)
tp=min(tp,edge[sta[i]].cap-edge[sta[i]].flow);
maxflow+=tp;
for(int 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]].nxt;
}
}
}
return maxflow;
}
int n,f,d;
int s,t,tot;
int main(){
//freopen("input.txt","r",stdin);
while(scanf("%d%d%d",&n,&f,&d)!=EOF){
init();
s=0;t=f+n+n+d+1;tot=t+1;
int x,y,num;
for(int i=1;i<=n;i++){
scanf("%d%d",&x,&y);
while(x--){
scanf("%d",&num);
addedge(num,f+i,1);
}
while(y--){
scanf("%d",&num);
addedge(f+i+n,f+n+n+num,1);
}
addedge(f+i,f+n+i,1);
}
for(int i=1;i<=f;i++) addedge(s,i,1);
for(int i=1;i<=d;i++) addedge(i+f+n+n,t,1);
printf("%d\n",dinic(s,t,tot));
}
return 0;
}
细节:
点的编号容易写错,注意在addedge的时候,带几个数值确定检验一下