费用流

 

MCMF:

 1 // #include <bits/stdc++.h>
 2 #include <iostream>
 3 #include <cstring>
 4 #include <string>
 5 #include <algorithm>
 6 #include <cmath>
 7 #include <cstdio>
 8 #include <queue>
 9 #include <stack>
10 #include <map>
11 #define ll long long
12 #define ull unsigned long long
13 #define met(a, b) memset(a, b, sizeof(a))
14 #define rep(i, a, b) for(int i = a; i <= b; i++)
15 #define bep(i, a, b) for(int i = a; i >= b; i--)
16 #define lowbit(x) (x&(-x))
17 #define MID (l + r) / 2
18 #define ls pos*2
19 #define rs pos*2+1
20 #define pb push_back
21 #define ios() ios::sync_with_stdio(0), cin.tie(0), cout.tie(0)
22 
23 using namespace std;
24 
25 const int maxn = 1e5 + 100;
26 const int inf = 0x3f3f3f3f;
27 const ll INF = 0x3f3f3f3f3f3f3f3f;
28 const ll mod = 998244353;
29 const double eps = 1e-2;
30 const double PI = 3.1415926;
31 
32 struct node {
33     int x, y;
34 }arr1[110], arr2[110];
35 struct EDGE {
36     int to, flow, cost, net;
37 }edge[maxn];
38 
39 int head[maxn], cnt;
40 int tail1, tail2;
41 int cost[maxn], pre[maxn], flow[maxn], w_edge[maxn], vis[maxn];
42 queue<int> que;
43 
44 void addedge(int u, int v, int flow, int cost) {
45     edge[cnt] = (EDGE){v, flow, cost, head[u]};
46     head[u] = cnt++;
47     edge[cnt] = (EDGE){u, 0, -cost, head[v]};
48     head[v] = cnt++;
49 }
50 int spfa(int s, int e) {
51     rep(i, s, e) {
52         cost[i] = inf;
53         pre[i] = -1;
54         flow[i] = inf;
55         vis[i] = 0;
56     }
57     que.push(s);
58     vis[s] = 1;
59     cost[s] = 0;
60     while(!que.empty()) {
61         int t = que.front();
62         que.pop();
63         vis[t] = 0;
64         for(int i = head[t]; i != -1; i = edge[i].net) {
65             int to = edge[i].to;
66             if(edge[i].flow && cost[t] + edge[i].cost < cost[to]) {
67                 cost[to] = cost[t] + edge[i].cost;
68                 pre[to] = t;
69                 w_edge[to] = i;
70                 flow[to] = min(flow[t], edge[i].flow);
71                 if(!vis[to]) {
72                     vis[to] = 1;
73                     que.push(to);
74                 }
75             }
76         }
77     }
78     if(pre[e] == -1) return 0;
79     else return 1;
80 }
81 void costflow(int s, int e) {
82     int maxflow = 0, mincost = 0;
83     while(spfa(s, e)) {
84         maxflow += flow[e];
85         mincost += flow[e] * cost[e];
86         int t = e;
87         while(t != s) {
88             edge[w_edge[t]].flow -= flow[e];
89             edge[w_edge[t] ^ 1].flow += flow[e];
90             t = pre[t];
91         }
92     }
93 }
 

 

posted @ 2020-05-27 21:15  Ruby·Z  阅读(75)  评论(0)    收藏  举报