1 #include <algorithm>
 2 #include <cstdio>
 3 #include <cctype>
 4 #include <queue>
 5 #define INF 0x3f3f3f3f
 6 #define MAXN 10010
 7 #define MAXM 300010
 8 using namespace std;
 9 int n, m, s, t, tot = 1;
10 int beginx[MAXN], endx[MAXM], nxt[MAXM], res[MAXM];
11 inline void add_edge(int u, int v, int w)
12 {
13     nxt[++tot] = beginx[u], beginx[u] = tot, endx[tot] = v, res[tot] = w;
14     nxt[++tot] = beginx[v], beginx[v] = tot, endx[tot] = u, res[tot] = 0;
15 }
16 struct PQ
17 {
18     int x,h;
19     PQ(int _x,int _h)
20     {
21         x = _x, h = _h;
22     }
23     bool operator <  (const PQ &tar) const
24     {
25         return h < tar.h;
26     }
27 };
28 int gap[MAXN], d[MAXN], ans[MAXN];
29 inline bool push(int x, int y, int ptr)
30 {
31     int w = min(ans[x], res[ptr]);
32     res[ptr] -= w, res[ptr^1] += w;
33     ans[x] -= w, ans[y] += w;
34     return w;
35 }
36 inline void Gap(int val)
37 {
38     for (int i = 1; i <= n; ++i)
39         if(i != s && i != t && val < d[i] && d[i] <= n)
40             d[i] = n + 1;
41 }
42 inline int HLPP()
43 {
44     priority_queue<PQ> pq;
45     d[s] = n, ans[s] = INF, pq.push(PQ(s, d[s]));
46     int u;
47     while(!pq.empty())
48     {
49         u = pq.top().x, pq.pop();
50         if(!ans[u]) continue;
51         for(int i = beginx[u], v = endx[i]; i; i = nxt[i], v = endx[i])
52             if((u == s || d[u] == d[v] + 1) && push(u, v, i) && v != t && v != s)
53                 pq.push(PQ(v, d[v]));
54         if (u != s && u != t && ans[u]) 
55         {
56             if(!(--gap[d[u]])) Gap(d[u]);
57             ++gap[++d[u]];
58             pq.push(PQ(u, d[u]));
59         }
60     }
61     return ans[t];
62 }
63 int main()
64 {
65     scanf("%d%d%d%d",&n,&m,&s,&t);
66     for(int i = 1; i <= m; i++)
67     {
68         int u,v,r;
69         scanf("%d%d%d",&u,&v,&r);
70         add_edge(u, v, r);
71     }
72     printf("%d", HLPP());
73     return 0;
74 }
HLPP

相关文章: