-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetflow.cpp
124 lines (110 loc) · 2.15 KB
/
netflow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include "log.h"
#include "netflow.h"
#include <cstring>
#define EPS 1e-8
namespace netflow {
Netflow::Netflow()
{
/* empty */
}
Netflow::~Netflow()
{
/* empty */
}
void Netflow::AddChrome(int from, int to, int flow, double v)
{
LogInfo("add edge %d -> %d = %d %lf\n", from, to, flow, v);
node[top].from = from;
node[top].to = to;
node[top].flow = flow;
node[top].v = v;
node[top].ne = head[from];
head[from] = top;
top ++;
}
void Netflow::Init(std::vector<function::Function> funA, std::vector<function::Function> funB)
{
n = funA.size() + funB.size() + 2;
m = 2 * (funA.size() + funB.size() + funA.size() * funB.size()); // 双向边
s = n - 1;
t = n;
top = 0;
memset(head, -1, sizeof(head));
LogInfo("n = %d m = %d s = %d t = %d\n", n, m, s, t);
}
int Netflow::Spfa()
{
for(int i = 0; i <= t; i ++){
inq[i] = 0;
dis[i] = INF;
pre[i] = -1;
}
mincost[s] = INF;
mincost[t] = 0;
queR = queL = 0;
dis[s] = 0;
minn[s] = INF;
inq[s] = true;
que[queR] = s;
queR ++;
while(queL != queR){
int now;
if(queL >= N || queR >= N) {
exit(0);
}
now = que[queL];
queL ++;
inq[now] = false;
for(int i = head[now]; i != -1; i = node[i].ne) {
int v;
/// cout << 123123 << endl;
v = node[i].to;
if(node[i].flow > 0 && dis[v] - dis[now] - node[i].v > EPS) {
dis[v] = dis[now] + node[i].v;
pre[v] = i; /// amazing 直接存边的编号
// printf("pre[%d] = %d %d\n", v, pre[v], i);
/// pre[v] = u;
mincost[v] = std::min(mincost[now], node[i].flow);
if(!inq[v]) {
inq[v] = true;
que[queR] = v;
queR ++;
}
}
}
}
return mincost[t];
}
double Netflow::Mincostmaxflow()
{
double ans;
int res;
ans = 0;
while(true){
res = Spfa();
if(!res){
break;
}
// printf("%d\n", res);
for(int i = pre[t]; i != -1; i = pre[node[i].from]) {
ans += res * node[i].v;
node[i].flow -= res;
node[i ^ 1].flow += res;
// printf("pre = %d %d %d\n", i, node[i].from, pre[node[i].from]);
// if(i == 48) {
// int ww; scanf("%d", &ww);
// }
//
}
}
return ans;
}
int Netflow::GetS()
{
return s;
}
int Netflow::GetT()
{
return t;
}
}