EK模版(邻接表版本)

邻接矩阵转邻接表,费了些时间啊。突然发现dinic用的好像是迭代深搜的思想把。

dinic写的有点问题,删了。

 EK邻接表版本

 1 #include <cstdio>

 2 #include <cstring>

 3 #include <cmath>

 4 #include <queue>

 5 using namespace std;

 6 #define INF 0x7fffffff

 7 int low[201],path[201];

 8 struct node

 9 {

10     int u,v,w,next,re;

11 }edge[201*201];

12 int first[201];

13 int str,end,n,m,t;

14 void CL()

15 {

16     t = 1;

17     memset(first,-1,sizeof(first));

18 }

19 void add(int u,int v,int w)

20 {

21     edge[t].u = u;

22     edge[t].v = v;

23     edge[t].w = w;

24     edge[t].re = t+1;

25     edge[t].next = first[u];

26     first[u] = t ++;

27     edge[t].u = v;

28     edge[t].v = u;

29     edge[t].w = 0;

30     edge[t].re = t-1;

31     edge[t].next = first[v];

32     first[v] = t-1;

33     first[v] = t++;

34 }

35 int bfs()

36 {

37     int u,v,i;

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

39     queue<int> que;

40     que.push(str);

41     low[str] = INF;

42     while(!que.empty())

43     {

44         u = que.front();

45         que.pop();

46         if(u == end) break;

47         for(i = first[u]; i != -1; i = edge[i].next)

48         {

49             v = edge[i].v;

50             if(v != str&&path[v] == -1&&edge[i].w)

51             {

52                 low[v] = low[u] < edge[i].w ? low[u]:edge[i].w;

53                 path[v] = i;

54                 que.push(v);

55             }

56         }

57     }

58     if(path[end] == -1)

59         return -1;

60     else

61         return low[end];

62 }

63 int EK()

64 {

65     int ans = 0,res,now,temp;

66     while((res = bfs()) != -1)

67     {

68         ans += res;

69         now = end;

70         while(now != str)

71         {

72             temp = path[now];

73             edge[edge[temp].re].w += res;

74             edge[temp].w -= res;

75             now = edge[temp].u;

76         }

77     }

78     return ans;

79 }

80 int main()

81 {

82     int i,sv,ev,w;

83     while(scanf("%d%d",&m,&n)!=EOF)

84     {

85         CL();

86         for(i = 1; i <= m; i ++)

87         {

88             scanf("%d%d%d",&sv,&ev,&w);

89             add(sv,ev,w);

90         }

91         str = 1,end = n;

92         printf("%d\n",EK());

93     }

94     return 0;

95 }

 

你可能感兴趣的:(模版)