Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ SPOJ FASTFLOW上的回答错误?_C++_Algorithm_Graph Theory - Fatal编程技术网

C++ SPOJ FASTFLOW上的回答错误?

C++ SPOJ FASTFLOW上的回答错误?,c++,algorithm,graph-theory,C++,Algorithm,Graph Theory,有人能帮我吗?我已经试了好几天了。我每次都得到错误的答案。我用了爱德蒙-卡普法。。。这是我的密码: #include<cstdio> #include<iostream> #include<queue> #include<algorithm> #include<cstring> #define MAXX 900000000 using namespace std; long

有人能帮我吗?我已经试了好几天了。我每次都得到错误的答案。我用了爱德蒙-卡普法。。。这是我的密码:

    #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;
    }
#包括
#包括
#包括
#包括
#包括
#定义max900000000
使用名称空间std;
长整数容量[5005][5005];
int图[5005][5005],v[5005],from[5005];
//使用Edmond karp计算最大流量
int最大流量(int s,int t)
{queueQ;
//Bfs获取从源到接收器的路径
Q.推送;
v[s]=1;
INTR;
长整敏;
而(!Q.empty())
{int p=Q.front();
Q.pop();
r=0;
对于(int j=0;图[p][j]!=0;j++)
{  
如果(!v[graph[p][j]]&容量[p][graph[p][j]])
{Q.push(图[p][j]);from[图[p][j]]=p;
v[图[p][j]]=1;
if(图[p][j]==t)
{r=1;中断;}
}
}
如果(r==1)
打破
}
r=t;
最小=最大;
//计算BFS找到的路径上的最小容量
while(从[r]!=0)
{ 
如果(最小>容量[从[r]][r])
最小值=容量[从[r]][r];
r=来自[r];
}
r=t;
//减去路径上找到的最小容量
while(从[r]!=0)
{ 
容量[从[r]][r]=min;
容量[r][from[r]]+=min;
r=来自[r];
}
如果(最小==最大)
返回0;
其他的
返回最小值;
}
int main()
{
int t,n,s,c,i,j,k,a,b,p=0;
无符号长整型流,r;
memset(容量,0,sizeof(容量));
memset(from,0,sizeof(from));
memset(图,0,sizeof(图));
memset(v,0,sizeof(v));
scanf(“%d%d”、&n和&c);

对于(i=0;i假设有一个简单的图,在开始和结束之间有一条边,容量为10亿

当您的MAXX<10亿时,当您运行Max_流时,您会发现一个MAXX流,并错误地得出结论,这意味着没有找到增广路径

如果是这种情况,则只需尝试更换

#define MAXX 900000000


程序可能会通过…

根据评论,您必须使用Dinic算法,但dinitz算法需要O(V^2.E)时间,这对于问题的约束非常高。谢谢:)…这可能是问题所在…因为现在我得到的是TLE而不是WA…我想现在我应该使用Dinic算法
#define MAXX 1100000000