poj 1273

网络流之最大流的基础题;

可以使用dinic算法和EK算法:

分别对着模板敲了一遍:

dinic:

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<queue>
 5 #define maxn 205
 6 #define inf 0xfffffff
 7 using namespace std;
 8 
 9 int map[maxn][maxn],level[maxn];
10 int n,m;
11 
12 int bfs(int s)
13 {
14     memset(level,0,sizeof level);
15     queue<int>q;
16     q.push(s);
17     level[s]=1;
18     while(!q.empty())
19     {
20         int now=q.front();
21         q.pop();
22         for(int i=1; i<=m; i++)
23             if(!level[i]&&map[now][i]>0)
24             {
25                 level[i]=level[now]+1;
26                 q.push(i);
27             }
28     }
29     return level[m]!=0;
30 }
31 
32 int dfs(int s,int cp)
33 {
34     int tmp=cp,t;
35     if(s==m) return cp;
36     for(int i=1; i<=m&&tmp; i++)
37         if(level[i]==level[s]+1&&map[s][i]>0)
38         {
39             t=dfs(i,min(tmp,map[s][i]));
40             map[s][i]-=t;
41             map[i][s]+=t;
42             tmp-=t;
43         }
44     return cp-tmp;
45 }
46 
47 int main()
48 {
49     int a,b,f;
50     while(scanf("%d%d",&n,&m)!=EOF)
51     {
52         memset(map,0,sizeof map);
53         for(int i=0; i<n; i++)
54         {
55             scanf("%d%d%d",&a,&b,&f);
56             map[a][b]+=f;
57         }
58         int ans=0,flow=0;
59         while(bfs(1))
60         {
61             while(flow=dfs(1,inf))
62                 ans+=flow;
63         }
64         printf("%d\n",ans);
65     }
66     return 0;
67 }
View Code

EK:

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #define maxn 205
 5 #include<queue>
 6 using namespace std;
 7 
 8 int map[maxn][maxn],p[maxn],n,m;
 9 bool flag[maxn];
10 bool bfs()
11 {
12     queue<int>q;
13     memset(flag,0,sizeof flag);
14     memset(p,-1,sizeof p);
15     q.push(1);
16     flag[1]=1;
17     while(!q.empty())
18     {
19         int now=q.front();
20         q.pop();
21         if(now==m) return 1;
22         for(int i=1; i<=m; i++)
23             if(map[now][i]>0&&!flag[i])
24             {
25                 flag[i]=1;
26                 p[i]=now;
27                 q.push(i);
28             }
29     }
30     return 0;
31 }
32 
33 void ek()
34 {
35     int u,flow=0,t;
36     while(bfs())
37     {
38         t=99999999;
39         u=m;
40         while(p[u]!=-1)
41         {
42             t=min(t,map[p[u]][u]);
43             u=p[u];
44         }
45         flow+=t;
46         u=m;
47         while(p[u]!=-1)
48         {
49             map[p[u]][u]-=t;
50             map[u][p[u]]+=t;
51             u=p[u];
52         }
53     }
54     printf("%d\n",flow);
55 }
56 
57 int main()
58 {
59     int a,b,f;
60     while(scanf("%d%d",&n,&m)!=EOF)
61     {
62         memset(map,0,sizeof map);
63         while(n--)
64         {
65             scanf("%d%d%d",&a,&b,&f);
66             map[a][b]+=f;
67         }
68         ek();
69     }
70     return 0;
71 }
View Code

 

 

posted @ 2013-09-12 14:28  Yours1103  阅读(175)  评论(0编辑  收藏  举报