ACM模板——网络流

 1 #include<bits/stdc++.h>
 2 #define _for(i,a,b) for(register int i = (a);i < b;i ++)
 3 #define _rep(i,a,b) for(register int i = (a);i > b;i --)
 4 #define INF 0x3f3f3f3f
 5 #define MOD 100000000
 6 #define maxn 1000003
 7 #define pb push_back
 8 #define debug() printf("Miku Check OK!\n")
 9 typedef long long ll;
10 
11 using namespace std;
12 typedef pair<int,int> P;
13 inline ll read()
14 {
15     ll ans = 0;
16     char ch = getchar(), last = ' ';
17     while(!isdigit(ch)) last = ch, ch = getchar();
18     while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
19     if(last == '-') ans = -ans;
20     return ans;
21 }
22 inline void write(ll x)
23 {
24     if(x < 0) x = -x, putchar('-');
25     if(x >= 10) write(x / 10);
26     putchar(x % 10 + '0');
27 }
28 int ver[maxn],Next[maxn],head[maxn],val[maxn];
29 //incf[i]为i在此趟BFS流过的流量 
30 int vis[maxn],incf[maxn],pre[maxn];
31 int n,m,s,t,tot,maxflow;
32 void add(int x,int y,int w)
33 {
34     ver[++tot] = y,Next[tot] = head[x],head[x] = tot,val[tot] = w;
35 }
36 bool bfs()
37 {
38     memset(vis,0,sizeof(vis));
39     queue<int> q;
40     q.push(s);vis[s] = 1;
41     incf[s] = INF;
42     while(!q.empty())
43     {
44         int x = q.front();q.pop();
45         for(int i = head[x]; i; i = Next[i])
46             if(val[i])
47             {
48                 int y = ver[i];
49                 if(vis[y]) continue;
50                 incf[y] = min(incf[x],val[i]);
51                 pre[y] = i;
52                 q.push(y);vis[y] = 1;
53                 if(y==t) return true;
54             }
55         
56     }
57     return false;
58 }
59 void update()
60 {
61     int x = t;
62     while(x != s)
63     {
64         int i = pre[x];
65         val[i] -= incf[t];
66         val[i^1] += incf[t];
67         x = ver[i^1];
68         
69     }
70     maxflow += incf[t];
71 }
72 int main()
73 {
74     n = read();m = read();s = read();t = read();
75     tot = 1;maxflow = 0;
76     _for(i,1,m+1)
77     {
78         int x = read();int y = read();int c = read();
79         add(x,y,c);add(y,x,0);
80     } 
81     while(bfs()) update();
82     write(maxflow);
83     return 0;
84 }
EK算法
 1 #include<bits/stdc++.h>
 2 #define _for(i,a,b) for(register int i = (a);i < b;i ++)
 3 #define _rep(i,a,b) for(register int i = (a);i > b;i --)
 4 #define INF 0x3f3f3f3f
 5 #define MOD 100000000
 6 #define maxn 1000003
 7 #define pb push_back
 8 #define debug() printf("Miku Check OK!\n")
 9 typedef long long ll;
10 
11 using namespace std;
12 typedef pair<int,int> P;
13 inline ll read()
14 {
15     ll ans = 0;
16     char ch = getchar(), last = ' ';
17     while(!isdigit(ch)) last = ch, ch = getchar();
18     while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
19     if(last == '-') ans = -ans;
20     return ans;
21 }
22 inline void write(ll x)
23 {
24     if(x < 0) x = -x, putchar('-');
25     if(x >= 10) write(x / 10);
26     putchar(x % 10 + '0');
27 }
28 int ver[maxn],Next[maxn],head[maxn],val[maxn];
29 int d[maxn];
30 int n,m,s,t,tot,maxflow;
31 void add(int x,int y,int w)
32 {
33     ver[++tot] = y,Next[tot] = head[x],head[x] = tot,val[tot] = w;
34 }
35 bool bfs()
36 {
37     memset(d,0,sizeof(d));
38     queue<int> q;
39     q.push(s);d[s] = 1;
40     while(!q.empty())
41     {
42         int x = q.front();q.pop();
43         for(int i = head[x]; i; i = Next[i])
44             if(val[i] && !d[ver[i]])
45             {
46                 q.push(ver[i]);
47                 d[ver[i]] = d[x]+1;
48                 if(ver[i]==t)
49                     return true;
50             }
51     }
52     return false;
53 }
54 int dinic(int x,int flow)
55 {
56     if(x==t) return flow;
57     // k为子节点增量 
58     int rest = flow, k;
59     for(int i = head[x]; i && rest; i = Next[i])
60     {
61         if(val[i] && d[ver[i]] == d[x]+1)
62         {
63             k = dinic(ver[i],min(rest,val[i]));
64             if(!k) d[ver[i]] = 0;
65             val[i] -= k;
66             val[i^1] += k;
67             rest -= k;
68         }
69     }
70     return flow - rest;
71 }
72 int main()
73 {
74     n = read();m = read();s = read();t = read();
75     tot = 1;maxflow = 0;
76     _for(i,1,m+1)
77     {
78         int x = read();int y = read();int c = read();
79         add(x,y,c);add(y,x,0);
80     } 
81     int flow = 0;
82     while(bfs()) 
83         while(flow = dinic(s,INF))
84             maxflow += flow;
85     write(maxflow);
86     return 0;
87 }
Dinic算法
#include<bits/stdc++.h>
#define _for(i,a,b) for(register int i = (a);i < b;i ++)
#define _rep(i,a,b) for(register int i = (a);i > b;i --)
#define INF 0x3f3f3f3f
#define MOD 100000000
#define maxn 100003
#define pb push_back
#define debug() printf("Miku Check OK!\n")
typedef long long ll;

using namespace std;
typedef pair<int,int> P;
inline ll read()
{
    ll ans = 0;
    char ch = getchar(), last = ' ';
    while(!isdigit(ch)) last = ch, ch = getchar();
    while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
    if(last == '-') ans = -ans;
    return ans;
}
inline void write(ll x)
{
    if(x < 0) x = -x, putchar('-');
    if(x >= 10) write(x / 10);
    putchar(x % 10 + '0');
}
int ver[maxn],Next[maxn],head[maxn],val[maxn],cost[maxn];
//incf[i]为i在此趟BFS流过的流量
int vis[maxn],incf[maxn],pre[maxn];
int n,m,s,t,tot,maxflow,ans;
int d[maxn];
void add(int x,int y,int w,int c)
{
    ver[++tot] = y,Next[tot] = head[x],head[x] = tot,val[tot] = w,cost[tot] = c;
    ver[++tot] = x,Next[tot] = head[y],head[y] = tot,val[tot] = 0,cost[tot] = -c;
}
bool spfa()
{
    memset(vis,0,sizeof(vis));
    memset(d,0x3f,sizeof(d));
    queue<int> q;
    q.push(s);
    vis[s] = 1;
    d[s] = 0;
    incf[s] = INF;
    while(!q.empty())
    {
        int x = q.front();
        q.pop();
        vis[x] = 0;
        for(int i = head[x]; i; i = Next[i])
            if(val[i])
            {
                int y = ver[i];
                if(d[y] > d[x] + cost[i])
                {
                    d[y] = d[x] + cost[i];
                    incf[y] = min(incf[x],val[i]);
                    pre[y] = i;
                    if(!vis[y])
                        vis[y] = 1,q.push(y);
                }
            }
    }
    if(d[t]==INF)
        return false;
    return true;
}
void update()
{
    int x = t;
    while(x != s)
    {
        int i = pre[x];
        val[i] -= incf[t];
        val[i^1] += incf[t];
        x = ver[i^1];
    }
    maxflow += incf[t];
    ans += d[t] * incf[t];
}
int main()
{
    n = read();
    m = read();
    s = 1;
    t = n+n;
    tot = 1;
    maxflow = 0;
    add(1,1+n,INF,0);
    add(n,n+n,INF,0);
    _for(i,2,n)
        add(i,i+n,1,0);
    _for(i,1,m+1)
    {
        int x = read();
        int y = read();
        int c = read();
        add(x+n,y,1,c);
    }
    while(spfa()) update();
    printf("%d %d",maxflow,ans);
    return 0;
}
最小费用最大流

 

posted @ 2019-10-07 14:05  Asurudo  阅读(161)  评论(0编辑  收藏  举报