Cod sursa(job #79069)

Utilizator raula_sanChis Raoul raula_san Data 20 august 2007 18:57:19
Problema Critice Scor 40
Compilator cpp Status done
Runda Arhiva de probleme Marime 4.88 kb
#include <cstdio>

#define max_n 1001
#define max_m 10001

#define inf_n 0x3f3f

struct list
{
       int nod;
       
       list *next;
       
} *g[max_n];

struct edge
{
       int i, j;
       
} e[max_m];

int n, m;
int f[max_n][max_n], cap[max_n][max_n], a[max_n][max_n], t[max_n], s[max_n], d[max_n];

void read();
void flow();
void dynamics();
void write();

void add(list *&, int);
void bellman(int [], int);

int bfs();

int main()
{
    read();
    flow();
    dynamics();
    write();
    
    return 0;
}

void read()
{
     FILE *in = fopen("critice.in", "rt");

     int k, i, j, c;
     
     fscanf(in, "%d %d", &n, &m);

     for(k=1; k<=m; ++k)
     {
              fscanf(in, "%d %d %d", &e[k].i, &e[k].j, &c);
              
              i = e[k].i;
              j = e[k].j;
              
              a[i][j] = k;
              
              add(g[i], j);
              add(g[j], i);
              
              cap[i][j] = c;
			  cap[j][i] = c;
     }
     
     fclose(in);
}

void flow()
{
     int i, j, r;
     
     while(bfs())
     {
                 for(i=1; i<n; ++i)
                          if(f[i][n] < cap[i][n])
                          {
                                     r = cap[i][n] - f[i][n];
                                     
                                     for(j=i; j!=1; j=t[j])
											  r = r < cap[t[j]][j] - f[t[j]][j] ? r : cap[t[j]][j] - f[t[j]][j];
                                              
                                     if(r)
                                     {
                                          f[i][n] += r;
                                          f[n][i] -= r;
                                          
                                          for(j=i; j!=1; j=t[j])
                                          {
                                                   f[t[j]][j] += r;
												   f[j][t[j]] += r;
                                          }
                                     }
                          }
                 
                 for(i=1; i<=n; ++i)
                          t[i] = 0;
     }
}

void dynamics()
{
     int i;
     
     for(i=1; i<=n; ++i)
              s[i] = d[i] = inf_n;
              
     s[1] = 0;
     d[n] = 0;
     
	 bellman(s, 1);
     bellman(d, n);
}

void write()
{
     FILE *out = fopen("critice.out", "wt");
     
     int k, i, j, r;
     
     for(k=1, r=0; k<=m; ++k)
     {
              i = e[k].i;
              j = e[k].j;
              
              if(!s[i] && !d[j] || !s[j] && !d[i])
                       ++ r;
     }
     
     fprintf(out, "%d\n", r);
     
     for(k=1, r=0; k<=m; ++k)
     {
              i = e[k].i;
              j = e[k].j;
              
              if(!s[i] && !d[j] || !s[j] && !d[i])
                       fprintf(out, "%d\n", k);
     }
     
     fclose(out);
}

void add(list *&g, int nod)
{
     list *p = new list;
     p -> nod = nod;
     p -> next = g;
     g = p;
}

void bellman(int x[], int source)
{
     list *q, *eoq, *p;
     
     int nod, k;
     
     q = new list;
     q -> nod = source;
     q -> next = NULL;
     eoq = q;
     
     while(q != NULL)
     {
             nod = q -> nod;
             
             for(p=g[nod]; p!=NULL; p=p->next)
             {
                           k = f[nod][p -> nod] == cap[nod][p -> nod];
                           
                           if(x[nod] + k < x[p -> nod])
						   {
									 x[p -> nod] = x[nod] + k;

                                     eoq -> next = new list;
                                     eoq = eoq -> next;
                                     eoq -> nod = p -> nod;
                                     eoq -> next = NULL;
                           }
             }
             
             p = q;
             q = q -> next;
             delete p;
     }
}

int bfs()
{
    list *q, *eoq, *p;
    
    int nod;
    
    q = new list;
    q -> nod = 1;
    q -> next = NULL;
    eoq = q;
    
    while(q != NULL)
    {
            nod = q -> nod;
            
            for(p=g[nod]; p!=NULL; p=p->next)
            {
						  if(f[nod][p->nod] < cap[nod][p->nod] && t[nod] != p -> nod && !t[p -> nod])
                          {
                                            eoq -> next = new list;
                                            eoq = eoq -> next;
                                            eoq -> nod = p -> nod;
                                            eoq -> next = NULL;
                                            
                                            t[p -> nod] = nod;
                          }
            }
            
            p = q;
            q = q -> next;
            delete p;
    }
    
    return t[n];
}