HDU3549(2)+EK算法

View Code
 1  #include<cstdio>

 2  #include<cstring>

 3  #include<cstdlib>

 4  #include<queue>

 5  using namespace std;

 6  #define N 1005

 7  #define inf 9999999

 8  int map[N][N],path[N],flow[N];

 9  int start,eend,ans,n,m;

10  

11  int bfs(){

12     int t,i;

13     queue<int>q;

14     while(!q.empty())q.pop();

15      memset(path,-1,sizeof(path));

16     path[start]=0;

17     flow[start]=inf;

18     q.push(start);

19     while(!q.empty()){

20         t=q.front();

21         q.pop();

22         if(t==eend)break;

23         for(i=start;i<=eend;i++){//n

24             if(i!=start&&path[i]==-1&&map[t][i]){

25                 flow[i]=flow[t]<map[t][i]?flow[t]:map[t][i];

26                 path[i]=t;

27                 q.push(i);

28             }

29         }

30     }

31     if(path[eend]!=-1)return flow[eend];

32     else return -1;

33 }

34 void ek(){

35     

36     int now,pre,delta;

37     while((delta=bfs())!=-1){

38         ans+=delta;

39         now=eend;

40         while(now!=start){

41             pre=path[now];

42             map[pre][now]-=delta;

43             map[now][pre]+=delta;

44             now=pre;

45         }

46     }

47     return ;

48 }

49  

50  int main()

51  {

52      int i,j,k,tcase,t;

53      int a,b,c;

54      scanf("%d",&tcase);

55      for(t=1;t<=tcase;t++)

56      {

57          scanf("%d%d",&n,&m);

58          start=1;

59          eend=n;

60          memset(map,0,sizeof(map));

61          while(m--)

62          {

63              scanf("%d%d%d",&a,&b,&c);

64              map[a][b]+=c;

65          }

66          ans=0;

67          ek();

68          printf("Case %d: %d\n",t,ans);

69      }

70      return 0;

71  }

EK算法

 

不过还是有点不太明白,EK和Ford—fulkson算法有什么区别。。。。。。。。

你可能感兴趣的:(HDU)