-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaxflow.sublime-snippet
66 lines (61 loc) · 1.64 KB
/
maxflow.sublime-snippet
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
<snippet>
<content><![CDATA[
const int SZ=210; // no. of nodes
vector<int> adj[SZ];
int cap[SZ][SZ];
class prioritize{
public:
bool operator ()(pair<int,int>& p1,pair<int,int> &p2)
{
return p1.second<p2.second;
}
};
struct maxFlow{
int path,result=0,source,sink,from[SZ],vis[SZ],curr,tmp;
int max_flow()
{
while(bfs())
{
path=INFINITY;
for(int v=sink;v!=source;v=from[v]) path=min(path,cap[from[v]][v]);
for(int v=sink;v!=source;v=from[v]) cap[from[v]][v]-=path,cap[v][from[v]]+=path;
result+=path;
}
return result;
}
void addedge(int a,int b,int capacity)
{
adj[a].pb(b),cap[a][b]=capacity;
adj[b].pb(a),cap[b][a]=0;
}
bool bfs()
{
for(int i=0;i<SZ;i++) vis[i]=0;
priority_queue<pair<int,int> ,vector<pair<int,int> >,prioritize> q;
q.push({source,0}),vis[source]=1,from[source]=-1;
while(!q.empty())
{
curr=q.top().first,q.pop(),tmp=(int)(adj[curr].size());
for(int i=0;i<tmp;i++)
if(!vis[adj[curr][i]] and cap[curr][adj[curr][i]])
{
q.push({adj[curr][i],cap[curr][adj[curr][i]]}),from[adj[curr][i]]=curr,vis[adj[curr][i]]=1;
if(adj[curr][i]==sink) return true;
}
}
return false;
}
};
/*
maxFlow flow;
flow.source=0;
flow.sink=201;
flow.addedge(i,j,weight); // declares global cap[i][j]
flow.max_flow(); //changes global cap[i][j];
*/
]]></content>
<!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
<tabTrigger>_maxflow</tabTrigger>
<!-- Optional: Set a scope to limit where the snippet will trigger -->
<!-- <scope>source.python</scope> -->
</snippet>