Cod sursa(job #605600)

Utilizator BlaugranasEnal Gemaledin Blaugranas Data 1 august 2011 12:41:07
Problema Flux maxim de cost minim Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.47 kb
#include<stdio.h>
#include<string.h>
#define N 351
int n,m,k,j,i,l,s,d,t,w,q,c[N][N],f[N][N],p[N],h[N],r[N],g[N][N],a[N],o[N][N],b[N];
long e;
int main()
{freopen("fmcm.in","r",stdin),freopen("fmcm.out","w",stdout);
scanf("%d%d%d%d",&n,&m,&s,&d);
while(m--)
          scanf("%d%d%d%d",&i,&j,&l,&k),g[i][a[i]++]=j,g[j][a[j]++]=i,o[i][j]=k,o[j][i]=-k,c[i][j]=l;
while(1)      
          {memset(b,10001,sizeof(b)),memset(r,0,sizeof(r)),b[s]=p[d]=0,h[1]=s,m=10001,l=1;
          while(l)
                   {i=h[1],r[i]=0,h[1]=h[l--],j=1;
                   while((t=j<<1)<=l)
                          {if(t<l&&b[h[t+1]]<b[h[t]])
                                 t++;
                          if(b[h[j]]<=b[h[t]])
                                 break;
                          h[j]^=h[t]^=h[j]^=h[t],j=t;}
                   for(k=0;k<a[i];k++)
                   if(b[j=g[i][k]]>b[i]+o[i][j]&&c[i][j]>f[i][j])
                          {b[j]=b[i]+o[i][j],p[j]=i;
                          if(!r[j])
                                 {h[++l]=j,r[j]=1;
                                 for(t=l;t>2&&b[h[t]]<b[h[w=t>>1]];t=w)
                                        h[t]^=h[w]^=h[t]^=h[w];}}}
          if(!p[d])
                   break;
          for(l=d;l!=s;l=p[l])
          if(m>(k=c[p[l]][l]-f[p[l]][l]))
                   m=k;
          for(l=d;l!=s;l=p[l])
                   f[p[l]][l]+=m,f[l][p[l]]-=m;
          e+=b[d]*m;}
printf("%ld",e);
return 0;}