这道题调了很长的时间,本来以为可以20分钟A掉的,结果一直查错查啊查,一个多小时后非常崩溃地发现了自己的错误,大概是这么个类型的错误:
for(register int i=1;i<=T;i++){
for(register int i=1;i<=M;i++)
}
心情久久不能平静
就是一道普通的拆点网络流,源点连食物,食物连牛,牛连饮料,饮料连汇点,其中牛需要拆点,因为一头牛只能吃一种食物喝一种饮料
#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#define MAXN 100000+10
using namespace std;
struct Line{
int from,to,nxt,flow;
}line[MAXN];
int head[MAXN],tail=0,level[MAXN];
int S,T,N,F,D,cur[MAXN];
void add_line(int from,int to,int flow){
//tail++;
//line[tail].from=from;
line[tail].to=to;
line[tail].flow=flow;
line[tail].nxt=head[from];
head[from]=tail++;
}
bool bfs(){
memset(level,-1,sizeof(level));
queue<int>q;
q.push(S);
level[S]=0;
while(!q.empty()){
int u=q.front();q.pop();
for(register int i=head[u];i!=-1;i=line[i].nxt){
int v=line[i].to;
if(level[v]==-1&&line[i].flow){
level[v]=level[u]+1;
q.push(v);
}
}
}
if(level[T]==-1) return false;
else return true;
}
int dfs(int u,int maxflow){
int res=0;
if(u==T||maxflow==0) return maxflow;
for(register int i=head[u];i!=-1;i=line[i].nxt){
int v=line[i].to;
if(line[i].flow&&level[v]==level[u]+1){
int flow=dfs(v,min(maxflow,line[i].flow));
if(flow>0){
res+=flow;
line[i].flow-=flow;
line[i^1].flow+=flow;
maxflow-=flow;
if(maxflow==0) break;
}
}
}
if(res==0) level[u]=-1;
return res;
}
void solve(){
int cnt=0;
while(bfs()){
cnt+=dfs(S,0x7fffffff);
}
printf("%d\n",cnt);
}
int main(){
//freopen("1711.txt","r",stdin);
//freopen(".out","w",stdout);
scanf("%d%d%d",&N,&F,&D);
T=F+D+N+N+1;S=0;
memset(head,-1,sizeof(head));
for(register int i=F+1;i<=F+N;i++){
add_line(i,i+N,1);
add_line(i+N,i,0);
}
for(register int i=1;i<=N;i++){
int food,drink;
scanf("%d%d",&food,&drink);
for(register int j=1;j<=food;j++){
int fff;
scanf("%d",&fff);
add_line(fff,F+i,1);
add_line(F+i,fff,0);
}
for(register int j=1;j<=drink;j++){
int ddd;
scanf("%d",&ddd);
add_line(F+i+N,N+F+N+ddd,1);
add_line(N+F+ddd+N,F+i+N,0);
}
}
for(register int i=1;i<=F;i++){
add_line(S,i,1);
add_line(i,S,0);
}
for(register int i=N+F+N+1;i<=N+F+N+D;i++){
add_line(i,T,1);
add_line(T,i,0);
}
solve();
return 0;
}