谁能帮我解决这个问题,我正在尝试几天。我每次都得到错误的答案。我使用了 Edmonds - Karp 方法......这是我的代码:
#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;
}
正如问题陈述所说:“请注意,可能存在重复的边,以及从节点到自身的边”。所以我忽略了自循环,并在与这些节点相对应的“容量”数组中添加了重复边的容量。我创建了一个“图表”并从源到接收器执行 BFS 以获取路径,直到所有路径都被扩充。我总结了所有找到的最小值并打印了答案......谁能解释为什么错误的答案?