SlashGeek
SlashGeek

Reputation: 575

Wrong answer on SPOJ FASTFLOW?

Can anyone help me out with this problem I am trying it for days . I am getting wrong answer every time . I used Edmonds - Karp method ... Here is my code :

    #include<cstdio>
    #include<iostream>
    #include<queue>
    #include<algorithm>
    #include<cstring>
    #define MAXX 900000000
    using namespace std;
    long int capacity[5005][5005] ;
    int  graph[5005][5005] , v[5005] , from[5005] ;
     //Calculating Max Flow using Edmond karp 
     int Max_Flow(int s , int t)
    {  queue<int>Q ;
      // Bfs to get the paths from source to sink
       Q.push(s) ;
       v[s] = 1 ;
       int r ;
       long long int min ; 
       while(!Q.empty())
       {  int p = Q.front() ;
          Q.pop();
          r = 0 ;  
          for(int j = 0 ; graph[p][j]!=0 ; j++)
          {  
            if(!v[graph[p][j]]&&capacity[p][graph[p][j]])
            { Q.push(graph[p][j]) ; from[graph[p][j]] = p ;
              v[graph[p][j]] = 1 ;
              if(graph[p][j]==t)
              { r = 1 ; break ; }
            }

          }
          if(r==1)
          break ;
       }
       r = t ;
       min = MAXX ;
      // Caculating the minimum capacity over the path found by BFS        
       while(from[r]!=0)
       { 
         if(min>capacity[from[r]][r])
         min = capacity[from[r]][r] ;
         r = from[r] ;
       }
       r = t ;
       //Subtracting the min capacity found over the path
       while(from[r]!=0)
       { 
         capacity[from[r]][r]-=min;
         capacity[r][from[r]]+=min;
         r = from[r] ;
       }
       if(min==MAXX)
       return 0;
       else
       return min;
    }
    int main()
    {
         int t , n , s , c , i , j , k  , a , b  , p = 0 ; 
         unsigned long long int flow , r  ; 
           memset(capacity,0,sizeof(capacity));
           memset(from,0,sizeof(from));
           memset(graph,0,sizeof(graph));
           memset(v,0,sizeof(v));
           scanf("%d%d",&n,&c);
           for(i = 0 ; i<c ; i++)
           {
                 scanf("%d%d%d",&a,&b,&k);
                 if(b!=a) 
                 {
                 capacity[a][b]+=k ;
                 capacity[b][a]+=k ;
                 j = 0 ;
                 r = 0 ; 
                 while(graph[a][j]!=0) 
                 { if(graph[a][j]==b) 
                   { r = 1 ; break ; }
                   j++;
                 }
                 if(!r) graph[a][j] = b ;

                 j = 0 ;
                 r = 0 ; 
                 while(graph[b][j]!=0) 
                 { if(graph[b][j]==a) 
                   { r = 1 ; break ; }
                   j++;
                 }
                 if(!r) graph[b][j] = a ;
                 }

           }
           flow = 0 ;    
           r = 1 ; 
           while(r)
           { flow+=r ;
             r = Max_Flow(1,n) ;
             memset(from,0,sizeof(from));
             memset(v,0,sizeof(v));
           }
           printf("%lld\n",flow-1);


           return 0;
    }

As the problem statement says : "Note that it is possible for there to be duplicate edges, as well as an edge from a node to itself" . So I ignored the self loops and added the capacity of repeated edges in the 'capacity' array corresponding to those nodes . I created a 'graph' and performed BFS from source to sink to get paths until all the paths have been augmented . I summed up all min values found and printed the answer ... Can anyone explain why wrong answer ?

Upvotes: 2

Views: 544

Answers (1)

Peter de Rivaz
Peter de Rivaz

Reputation: 33499

Suppose you had a simple graph with a single edge between start and end with capacity 1 billion.

As your MAXX < 1 billion, when you run the Max_Flow you would find a flow of MAXX and incorrectly conclude that this meant there was no augmenting path found.

If this is the case, then simply try replacing

#define MAXX 900000000

with

#define MAXX 1100000000

and the program might pass...

Upvotes: 1

Related Questions