Pagini recente » Cod sursa (job #70523) | Cod sursa (job #1563975) | Cod sursa (job #2367172) | Cod sursa (job #902873) | Cod sursa (job #420885)
Cod sursa(job #420885)
#include<stdio.h>
FILE *f,*g;
long card1,card2,m,n,i,x,y,c[20000],fin[20000],t[4][200000],aux,ind,parinte[20000],ok,flux,min,nr,u,pr,st[20000],viz[20000],k,v[20000],p;
int bf()
{ pr=u=1; st[1]=1; viz[1]=ind; k=0;
while(pr<=u)
{ p=c[st[pr]];
while(p!=0)
{ if(viz[t[1][p]]!=ind&&t[3][p])
if(t[1][p]!=n) { u++; st[u]=t[1][p]; parinte[t[1][p]]=st[pr]; viz[t[1][p]]=ind; }
else { k++; v[k]=st[pr]; }
p=t[2][p];
}
pr++;
}
return k;
}
int main()
{ f=fopen("cuplaj.in","r"); g=fopen("cuplaj.out","w");
fscanf(f,"%ld%ld%ld",&card1,&card2,&m); n=card1+card2+2;
for(i=2;i<=card1+1;i++)
{ x=1; y=i;
if(c[x]==0) { nr++; c[x]=nr; fin[x]=nr; t[1][nr]=y; t[3][nr]=1; }
else { nr++; t[2][fin[x]]=nr; t[1][nr]=y; fin[x]=nr; t[3][nr]=1;}
aux=x; x=y; y=aux;
if(c[x]==0) { nr++; c[x]=nr; fin[x]=nr; t[1][nr]=y; }
else { nr++; t[2][fin[x]]=nr; t[1][nr]=y; fin[x]=nr; }
}
for(i=card1+2;i<=card1+1+card2;i++)
{ x=i; y=card1+card2+2;
if(c[x]==0) { nr++; c[x]=nr; fin[x]=nr; t[1][nr]=y; t[3][nr]=1; }
else { nr++; t[2][fin[x]]=nr; t[1][nr]=y; fin[x]=nr; t[3][nr]=1;}
aux=x; x=y; y=aux;
if(c[x]==0) { nr++; c[x]=nr; fin[x]=nr; t[1][nr]=y; }
else { nr++; t[2][fin[x]]=nr; t[1][nr]=y; fin[x]=nr; }
}
for(i=1;i<=m;i++)
{ fscanf(f,"%ld%ld",&x,&y); x++; y=card1+1+y;
if(c[x]==0) { nr++; c[x]=nr; fin[x]=nr; t[1][nr]=y; t[3][nr]=1; }
else { nr++; t[2][fin[x]]=nr; t[1][nr]=y; fin[x]=nr; t[3][nr]=1;}
aux=x; x=y; y=aux;
if(c[x]==0) { nr++; c[x]=nr; fin[x]=nr; t[1][nr]=y; }
else { nr++; t[2][fin[x]]=nr; t[1][nr]=y; fin[x]=nr; }
}
ind=1;
while(bf())
{ for(i=1;i<=k;i++)
{ x=n; parinte[n]=v[i]; min=1000000000;
while(x!=1)
{ p=c[parinte[x]]; ok=1;
while(p!=0&&ok)
{ if(t[1][p]==x) { if(t[3][p]<min) min=t[3][p]; ok=0; }
p=t[2][p];
}
x=parinte[x];
}
x=n;
while(x!=1)
{ p=c[parinte[x]]; ok=1;
while(p!=0&&ok)
{ if(t[1][p]==x) { t[3][p]-=min; if(p%2) t[3][p+1]+=min; else t[3][p-1]+=min; }
p=t[2][p];
}
x=parinte[x];
}
flux+=min;
}
ind++; parinte[n]=0;
}
fprintf(g,"%ld\n",flux);
/*for(i=2;i<=card1+1;i++)
{ p=c[i]; while(p!=0) { if(t[3][p]==0&&card1+2<=t[1][p]&&t[1][p]<=card1+card2+1) fprintf(g,"%ld %ld\n",i-1,t[1][p]-1-card1); p=t[2][p]; } }*/
fclose(g);
return 0;
}