Cod sursa(job #13015)

Utilizator devilkindSavin Tiberiu devilkind Data 5 februarie 2007 14:03:38
Problema Critice Scor 60
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.57 kb
#include <stdio.h>
#include <string.h>
#define NMAX 1001
#define MMAX 10001

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

struct lista{long int c,f,nod;
             lista *urm;} *vf[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(f,"%ld %ld",&n,&m);
lista *p;
for (i=1;i<=m;i++)
    {fscanf(f,"%ld %ld %ld",&x,&y,&c);
    
    p=new lista;
    p->f=0;
    p->c=c;
    p->nod=y;
    p->urm=vf[x];
    vf[x]=p;
    
    p=new lista;
    p->f=0;
    p->c=c;
    p->nod=x;
    p->urm=vf[y];
    vf[y]=p;
    
    muc[i][0]=x;
    muc[i][1]=y;
    }
}

int NS(long int x, long int y)
{
lista *p;
p=vf[x];
while (p->nod!=y)
     p=p->urm;
if (p->f<p->c) return 1;
return 0;
}


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

void ADD(long int x, long int y, long int val)
{
lista *p;
p=vf[x];
while (p->nod!=y)
     p=p->urm;
p->f+=val;     

p=vf[y];
while (p->nod!=x)
     p=p->urm;
p->f-=val;    
}
       
void flux()
{
long int ok=1,min,aux;
while (ok)
      {
      k=0;
      memset(marc,0,sizeof(marc));
      ok=drum(1);
      if (ok)
         {
	 min=10000;
         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;
lista *p;
p=vf[nod];
while (p)
      {
      if (!marc[p->nod]&&(NS(nod,p->nod)&&NS(p->nod,nod))) DF(p->nod,rez);
      p=p->urm;
      }      
}   
         
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(f);
fclose(g);
return 0;
}