Cod sursa(job #13043)

Utilizator devilkindSavin Tiberiu devilkind Data 5 februarie 2007 14:46:43
Problema Critice Scor 70
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.35 kb
#include <stdio.h>
#include <string.h>
#define NMAX 1005
#define MMAX 10005

FILE *h = fopen("critice.in","rt"), *g = fopen("critice.out","wt");

long int vf[NMAX][NMAX],cap[NMAX][NMAX],f[NMAX][NMAX];   
    
long int n,i,j,k,src[NMAX],dest[NMAX],x,y,m,c,muc[MMAX][2],marc[NMAX],crit[MMAX];
long int st[NMAX];
         
void citire()
{
fscanf(h,"%ld %ld",&n,&m);
for (i=1;i<=m;i++)
    {fscanf(h,"%ld %ld %ld",&x,&y,&c);
    
    vf[x][0]++;
    f[x][y]=0;
    cap[x][y]=c;
    vf[x][vf[x][0]]=y;
    
    vf[y][0]++;
    f[y][x]=0;
    cap[y][x]=c;
    vf[y][vf[y][0]]=x;
    
    muc[i][0]=x;
    muc[i][1]=y;
    }
}

int NS(long int x, long int y)
{
if (f[x][y]<cap[x][y]) return 1;
return 0;
}


int drum(long int nod)
{
long int ok=0;
st[++k]=nod;
marc[nod]=1;
if (nod==n) return 1;
int p;
p=vf[nod][0];
while (p>0)
      {
      if (!marc[vf[nod][p]]&&NS(nod,vf[nod][p])) ok=drum(vf[nod][p]);
      if (ok) return 1;
      p--;
      }
k--;
return 0;
}
      
      
long int difer(long int x,long int y)
{
return cap[x][y]-f[x][y];
} 

void ADD(long int x, long int y, long int val)
{
f[x][y]+=val;     
f[y][x]-=val;  
}
       
void flux()
{
long int ok=1,min,aux;
while (ok)
      {
      k=0;
      memset(marc,0,sizeof(marc));
      ok=drum(1);
      if (ok)
         {
	 min=100001;
         for (i=1;i<=k-1;i++)
             {aux=difer(st[i],st[i+1]);
             if (aux<min) min=aux;
             }
         for (i=1;i<=k-1;i++)
             ADD(st[i],st[i+1],min);
         }
      }
}
   
void DF(long int nod,long int rez)
{
marc[nod]=1;
if (rez) src[nod]=1;
   else dest[nod]=1;
long int p;
p=1;
while (p<=vf[nod][0])
      {
      if (!marc[vf[nod][p]]&&(NS(nod,vf[nod][p])&&NS(vf[nod][p],nod))) DF(vf[nod][p],rez);
      p++;
      }      
}   
         
void solve()
{
long int x,y,nrs=0;
for (i=1;i<=m;i++)
    if ((!NS(muc[i][0],muc[i][1]))||(!NS(muc[i][1],muc[i][0])))
       {x=muc[i][0];
       y=muc[i][1];
       if ((src[x]&&dest[y])||(dest[x]&&src[y])) {nrs++;crit[nrs]=i;}
       }
fprintf(g,"%ld\n",nrs);
for (i=1;i<=nrs;i++)
    fprintf(g,"%ld\n",crit[i]);
}
         
             
int main()
{
citire();
flux();
memset(marc,0,sizeof(marc));
DF(1,1);
memset(marc,0,sizeof(marc));
DF(n,0);
solve();
fclose(h);
fclose(g);
return 0;
}