P3381 模板最小费用最大流

Posted thjkhdf12

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了P3381 模板最小费用最大流相关的知识,希望对你有一定的参考价值。

技术图片
  1 #include<iostream>
  2 #include<queue>
  3 #include<cstring>
  4 #include<cstdio>
  5 using namespace std;
  6 
  7 #define Design ios::sync_with_stdio(0),cin.tie(0),cout.tie(0)
  8 #define mfor(i,a,b) for(register int i=(a);i<=(b);i++)
  9 
 10 const int maxn = 5010;
 11 const int maxm = 100010;
 12 const int INF = 0x3f3f3f3f;
 13 int cnt;
 14 int n, m;
 15 int h[maxn];
 16 int prep[maxn];
 17 int pree[maxm];
 18 int dis[maxn];
 19 int st = maxn - 2;
 20 int ed = maxn - 1;
 21 
 22 struct Edge
 23 {
 24     int v, nxt;
 25     int w, f;
 26 }e[maxm];
 27 
 28 int head[maxn];
 29 
 30 inline void init()
 31 {
 32     memset(head, -1, sizeof(head));
 33     cnt = 1;
 34 }
 35 
 36 inline void add(int u, int v, int w, int f)
 37 {
 38     e[++cnt].v = v;
 39     e[cnt].nxt = head[u];
 40     head[u] = cnt;
 41     e[cnt].f = f;
 42     e[cnt].w = w;
 43     e[++cnt].v = u;
 44     e[cnt].nxt = head[v];
 45     head[v] = cnt;
 46     e[cnt].f = -f;
 47     e[cnt].w = 0;
 48 }
 49 
 50 inline pair<int, int> dijstra()
 51 {
 52     int maxf = 0;
 53     int minc = 0;
 54     while (1)
 55     {
 56         priority_queue<pair<int, int>>heap;
 57         memset(dis, 0x3f, sizeof(dis));
 58         dis[st] = 0;
 59         heap.push(make_pair(0, st));
 60         while (!heap.empty())
 61         {
 62             pair<int, int> x = heap.top();
 63             heap.pop();
 64             if (-x.first != dis[x.second]) continue;
 65             if (x.second == ed) break;
 66             for (int i = head[x.second]; i != -1; i = e[i].nxt)
 67             {
 68                 int now = e[i].f + h[x.second] - h[e[i].v];
 69                 if (e[i].w > 0 && dis[e[i].v] > dis[x.second] + now)
 70                 {
 71                     dis[e[i].v] = dis[x.second] + now;
 72                     heap.push(make_pair(-dis[e[i].v], e[i].v));
 73                     prep[e[i].v] = x.second;
 74                     pree[e[i].v] = i;
 75                 }
 76             }
 77         }
 78         if (dis[ed] >= 0x3f3f3f3f) break;
 79         for (int i = 0; i <= n; i++) h[i] += dis[i];
 80         int now = 0x3f3f3f3f;
 81         for (int i = ed; i != st; i = prep[i])
 82             now = min(now, e[pree[i]].w);
 83         for (int i = ed; i != st; i = prep[i])
 84         {
 85             e[pree[i]].w -= now;
 86             e[pree[i] ^ 1].w += now;
 87         }
 88         maxf += now;
 89         minc += now * h[ed];
 90     }
 91     return make_pair(maxf, minc);
 92 }
 93 
 94 int main()
 95 {
 96     Design;
 97     init();
 98     cin >> n >> m;
 99     cin >> st >> ed;
100     mfor(i, 1, m)
101     {
102         int u, v, w, f;
103         cin >> u >> v >> w >> f;
104         add(u, v, w, f);
105     }
106     pair<int, int>ans = dijstra();
107     cout << ans.first << " " << ans.second;
108 }
View Code

 

以上是关于P3381 模板最小费用最大流的主要内容,如果未能解决你的问题,请参考以下文章

P3381 模板最小费用最大流

最小费用最大流(luogu P3381 模板最小费用最大流)

luogu P3381 模板最小费用最大流 |网络流费用流

P3381 模板最小费用最大流

P3381 模板最小费用最大流

P3381 模板最小费用最大流