Cod sursa(job #768258)

Utilizator igsifvevc avb igsi Data 16 iulie 2012 14:46:23
Problema Flux maxim Scor 50
Compilator c Status done
Runda Arhiva educationala Marime 1.06 kb
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define max_n 1001

FILE *in, *out;
int c[max_n][max_n], t[max_n], n, m, flux;

int bf(int s,int d)
{
    int li, ls, nod, q[max_n], i;
    
    memset(t,0,sizeof(t));
    
    for(li=0, ls=0, t[s]=-1, q[0]=s; li<=ls; li++)
    {
       nod = q[li];
       for(i = 1; i<=n; i++)
          if(!t[i] && c[nod][i])
          {
             q[++ls] = i;
             t[i] = nod;
             if(i == d) return 1;
          }
    }
    return 0;
}

int main()
{
    int i,r,x,y,z;
    
    in = fopen("maxflow.in", "r");
    out = fopen("maxflow.out", "w");
    
    fscanf(in, "%d %d", &n, &m);
    for(i=1; i<=m; i++)
    {
   		fscanf(in, "%d %d %d", &x, &y, &z);
   		c[x][y]=z;
    }
    
    for(flux = 0; bf(1,n); flux += r)
    {
      r = 1 << 30;
      i = n;
      for(i = n; i!=1; i = t[i])
      	if(r > c[t[i]][i])
        	r = c[t[i]][i];
       
      for(i = n; i!=1; i = t[i])
      {
         c[t[i]][i]-=r;
      }
    }
    
    fprintf(out, "%d\n", flux);
    
    fclose(in);
    fclose(out);
    return 0;
}