Skip to content

Commit c27a4b7

Browse files
authored
未加优化的ISAP算法
1 parent 7bcce78 commit c27a4b7

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#include <cstdio>
2+
3+
#define INF 1000000000
4+
#define VERTEX_COUNT 1000
5+
#define EDGE_COUNT 1000
6+
7+
using namespace std;
8+
9+
struct vertex
10+
{
11+
int first, dis;
12+
}V[VERTEX_COUNT];
13+
14+
struct edge
15+
{
16+
int endp, next, flow;
17+
}E[EDGE_COUNT];
18+
19+
int ec = 2, iec, ivc, src, sink;
20+
21+
inline int min(int x, int y)
22+
{
23+
return x < y ? x : y;
24+
}
25+
26+
void add_edge(int u, int v, int f)
27+
{
28+
E[ec].next = V[u].first;
29+
V[u].first = ec;
30+
E[ec].endp = v;
31+
E[ec].flow = f;
32+
ec++;
33+
}
34+
35+
int DFS(int u, int curf)
36+
{
37+
if (u == sink)
38+
{
39+
return curf;
40+
}
41+
int totalf = 0, mindis = INF;
42+
for (int cur = V[u].first; cur != 0 && totalf < curf; cur = E[cur].next)
43+
{
44+
if (E[cur].flow > 0)
45+
{
46+
if (V[u].dis == V[E[cur].endp].dis + 1)
47+
{
48+
int t = DFS(E[cur].endp, min(curf - totalf, E[cur].flow));
49+
E[cur].flow -= t;
50+
E[cur ^ 1].flow += t;
51+
totalf += t;
52+
}
53+
mindis = min(mindis, V[E[cur].endp].dis);
54+
}
55+
}
56+
if (totalf == 0)
57+
{
58+
V[u].dis = mindis + 1;
59+
}
60+
return totalf;
61+
}
62+
63+
int max_flow()
64+
{
65+
int res = 0;
66+
while (V[src].dis < ivc)
67+
{
68+
res += DFS(src, INF);
69+
}
70+
return res;
71+
}
72+
73+
int main()
74+
{
75+
int u, v, f;
76+
scanf("%d%d", &iec, &ivc);
77+
for (int i = 0; i < iec; i++)
78+
{
79+
scanf("%d%d%d", &u, &v, &f);
80+
add_edge(u, v, f);
81+
add_edge(v, u, 0);
82+
}
83+
src = 1;
84+
sink = ivc;
85+
printf("%d", max_flow());
86+
return 0;
87+
}

0 commit comments

Comments
 (0)