Cod sursa(job #29957)

Utilizator t30Rosu Teodor t30 Data 11 martie 2007 22:20:16
Problema Critice Scor 100
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.83 kb
#include<stdio.h>
typedef struct nod { int x,c; nod *d; } nod;
typedef struct list { int x; list *s,*d;} list;
nod *v[1010],*a[1010];
list *l;

int n,m,h[1010],f[1010][1010],c[1010][1010],lx[10100],ly[10100],s[1010];
long e[1010],flux;

void add(int x,int y,int c)
{
  nod *p;
  p=new nod;
  p->x=y;
  p->c=c;
  p->d=v[x];
  v[x]=p;
}

void READ(){
FILE *f;
int i,x,y,C;

   f=fopen("critice.in","r");
   fscanf(f,"%d %d",&n,&m);

   for(i=1;i<=m;i++){
	fscanf(f,"%d %d %d",&x,&y,&C);
	c[x][y]=c[y][x]=C;
	lx[i]=x;
	ly[i]=y;
	add(x,y,i);
	add(y,x,i);
   }
   fclose(f);
}

void INIT(){
int i,j;

   for(i=1;i<=n;i++){
	h[i]=e[i]=0;
   }
   h[1]=n;

   for(i=1;i<=n;i++)
      for(j=1;j<=n;j++)
	 f[i][j]=0;


   nod *p;
   p=v[1];
   while(p){
     f[1][p->x]=c[1][p->x];
     f[p->x][1]=-f[1][p->x];
     e[p->x]=f[1][p->x];
     p=p->d;
   }

   list *ll;
   if(!l)
      for(i=n-1;i>1;i--){
	 ll=new list;
	 ll->d=l;
	 ll->s=0;
	 if(l) l->s=ll;
	 ll->x=i;
	 l=ll;
      }

   for(i=1;i<=n;i++)
      a[i]=v[i];
}


void RIDICA(int x){
nod *p;
int min=2*n;

  p=v[x];
  while(p){
     if(h[p->x]>=h[x] && h[p->x]+1<min) min=h[p->x]+1;
     p=p->d;
  }
h[x]=min;
}


void POMPEAZA(int x,int y){
int min;
min=(e[x]<c[x][y]-f[x][y]?e[x]:c[x][y]-f[x][y]);
f[x][y]+=min;
f[y][x]=-f[x][y];
e[x]-=min;
e[y]+=min;
}

void DESCARCA(int x){
int y;
  while(e[x])
     if(!a[x]) {
	a[x]=v[x];
	RIDICA(x);
     }
     else {
	y=a[x]->x;
	if(h[x]==h[y]+1 && c[x][y]-f[x][y]>0) POMPEAZA(x,y);
	else a[x]=a[x]->d;
     }
}


void FLUX(){
int x,hh;
list *L;

  L=l;

  while(L){
     x=L->x;
     hh=h[x];

     DESCARCA(x);
     if(hh<h[x]) { if(L->s) {
		       (L->s)->d=L->d;
		       if(L->d) (L->d)->s=L->s;
		       L->s=0;
		       L->d=l;
		       l->s=L;
		       l=L;
		  } }
     L=L->d;


  }

}

void flood(int x,int k){
int d[1010],ii,jj;
nod *p;

  d[1]=x; s[x]=k;
  ii=1;jj=1;
  while(ii<=jj){
	p=v[d[ii]];
	while(p){
	   if(!s[p->x] && c[d[ii]][p->x]>f[d[ii]][p->x])
	   { s[p->x]=k;
	     d[++jj]=p->x; }
	p=p->d;
	}
  ii++;
  }
}


int main()
{
   READ();
   INIT();
   FLUX();
   flux=e[n];

   int i;
   for(i=1;i<=n;i++) s[i]=0;
   flood(1,1);
   flood(n,-1);

   int nr=0;
   for(i=1;i<=m;i++)
       if(s[lx[i]]*s[ly[i]]==-1) {nr++; lx[i]=-1; }
 /*     else if(c[ lx[i] ][ ly[i] ]==f[ lx[i] ][ ly[i] ] || c[ lx[i] ][ ly[i] ]==-f[ lx[i] ][ ly[i] ]){
      c[ lx[i] ][ ly[i] ]++;
      c[ ly[i] ][ lx[i] ]++;
      INIT();
      FLUX();
      c[ lx[i] ][ ly[i] ]--;
      c[ ly[i] ][ lx[i] ]--;
      if(flux<e[n]) { lx[i]=-1; nr++;  }

   }*/

   FILE *g;
   g=fopen("critice.out","w");
   fprintf(g,"%d\n",nr);
   for(i=1;i<=m;i++)
     if(lx[i]==-1) fprintf(g,"%d\n",i);
   fclose(g);
return 0;
}