Cod sursa(job #881780)

Utilizator toranagahVlad Badelita toranagah Data 18 februarie 2013 16:46:21
Problema Flux maxim Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 5.59 kb
nclude <vector>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <ctime>
using namespace std;
 
#define MAXN 1001
#define INF 0x3f3f3f3f
#define forit(it, v) for (typeof ( (v).begin() ) it = (v).begin(); it != (v).end(); ++it)
  
  int N, M, flux[MAXN][MAXN], dad[MAXN];
  int coada[MAXN];
  vector<int> graph[MAXN];
   
   bool DFS() {
           coada[0] = 1; coada[1] = 1;
            
                while (coada[0]) {
                            int poz = (rand() % coada[0]) + 1;
                                    int nod = coada[poz];
                                     
                                             coada[poz] = coada[coada[0]--];
                                              
                                                      if (dad[N])
                                                                      return 1;
                                                                       
                                                                               forit(it, graph[nod]) {
                                                                                               if (!dad[*it] && flux[nod][*it]) {
                                                                                                                   dad[*it] = nod;
                                                                                                                                   coada[++coada[0]] = *it;
                                                                                                                                               }
                                                                                                                                                       }
                                                                                                                                                           }
                                                                                                                                                               return 0;
   }
   int main() {
        
            freopen("maxflow.in", "r", stdin);
                freopen("maxflow.out", "w", stdout);
                 
                     scanf("%d%d", &N, &M);
                      
                          srand(time(NULL));
                           
                               for (int i = 1; i <= M; ++i) {
                                           int x, y, c;
                                                   scanf("%d%d%d", &x, &y, &c);
                                                           graph[x].push_back(y); graph[y].push_back(x);
                                                                   flux[x][y] = c;
                                                                       }
                                                                        
                                                                            int maxflow = 0;
                                                                             
                                                                                 while (DFS()) {
                                                                                             int minim = INF;
                                                                                              
                                                                                                      for (int nod = N; nod != 1; nod = dad[nod])
                                                                                                                      minim = min (minim, flux[dad[nod]][nod]);
                                                                                                                              maxflow += minim;
                                                                                                                                      for (int nod = N; nod != 1; nod = dad[nod]) {
                                                                                                                                                      flux[dad[nod]][nod] -= minim;
                                                                                                                                                                  flux[nod][dad[nod]] += minim;
                                                                                                                                                                          }
                                                                                                                                                                           
                                                                                                                                                                                   memset(dad, 0, sizeof(dad));
                                                                                                                                                                                       }
                                                                                                                                                                                        
                                                                                                                                                                                            printf("%d\n", maxflow);
                                                                                                                                                                                                return 0;
   }