-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmax-flow.codesnippet
82 lines (77 loc) · 2.14 KB
/
max-flow.codesnippet
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
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDECodeSnippetCompletionPrefix</key>
<string>maxflow</string>
<key>IDECodeSnippetCompletionScopes</key>
<array>
<string>TopLevel</string>
</array>
<key>IDECodeSnippetContents</key>
<string>#define MAX_V 110
//辺を表す構造体 (行き先, 容量, 逆辺)
struct edge{int to, cap, rev;};
vector<int> e[MAX_V];
vector<vector<edge> > G(MAX_V);
int level[MAX_V];
int iter[MAX_V];
int A[MAX_V];
void addEdge(int from, int to, int cap){
G[from].push_back(edge{to, cap, int(G[to].size())});
G[to].push_back(edge{from, 0, int(G[from].size()-1)});
}
void bfs(int s){
fill(level,level+MAX_V,-1);
queue<int> que;
que.push(s);
level[s]=0;
while(!que.empty()){
int from = que.front(); que.pop();
for(auto to: G[from]){
edge &e = to;
if(e.cap > 0 && level[e.to] < 0) {
level[e.to]=level[from]+1;
que.push(e.to);
}
}
}
}
int dfs(int v, int t, int f){
if(v==t) return f;
FOR(i, iter[v],G[v].size()){
edge &e = G[v][i];
if(e.cap > 0 && level[v] < level[e.to]){
int d = dfs(e.to, t , min(f, e.cap));
if(d>0){
e.cap -= d;
G[e.to][e.rev].cap += d;
return d;
}
}
}
return 0;
}
int maxFlow(int s, int t) {
int flow = 0;
while (true) {
bfs(s);
if (level[t] < 0 ) return flow;
fill(iter,iter+MAX_V,0);
int f;
while ((f = dfs(s, t, INF)) > 0) flow += f;
}
}
</string>
<key>IDECodeSnippetIdentifier</key>
<string>04B3E976-5662-4C97-B020-1E3C8EA99084</string>
<key>IDECodeSnippetLanguage</key>
<string>Xcode.SourceCodeLanguage.C-Plus-Plus</string>
<key>IDECodeSnippetTitle</key>
<string>最大流・最小カット</string>
<key>IDECodeSnippetUserSnippet</key>
<true/>
<key>IDECodeSnippetVersion</key>
<integer>2</integer>
</dict>
</plist>