Skip to content

Commit

Permalink
Create Edmonds-karp.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
radicalparty authored Sep 6, 2024
1 parent 324a7ad commit 95179c8
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions ps/Graph/network_flow/Edmonds-karp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
int Edmond_Karp(){
int n, p; cin >> n >> p;
int st = 1, ed = 2;
vector<vector<int>> cap(n + 1, vector<int>(n + 1));
vector<vector<int>> flow(n + 1, vector<int>(n + 1));
vector<int> edge[n + 1];
while (p--){
int u, v, w = 1; cin >> u >> v;// >> w
cap[u][v] += w; edge[u].emplace_back(v); edge[v].emplace_back(u);
}
int answer = 0;
int parent[n + 1];

while (true){
queue<int> q; q.push(st);
memset(parent, -1, sizeof(parent));
//bfs로 다음 경로 구하기
while (!q.empty()){
int now = q.front(); q.pop();
for (int nxt: edge[now]){
if (cap[now][nxt] - flow[now][nxt] > 0 && parent[nxt] == -1){
q.push(nxt); parent[nxt] = now;
if (nxt == ed) break;
}
}
}
//만약 더 이상 augmenting path가 존재하지 않으면 maximum flow
if (parent[ed] == -1) break;


int mn = 1e9;
for (int i = ed; i != st; i = parent[i]) mn = min(mn, cap[parent[i]][i] - flow[parent[i]][i]);
for (int i = ed; i != st; i = parent[i]) {
flow[parent[i]][i] += mn; //잔여 네트워크에서 flow 흘림
flow[i][parent[i]] -= mn;
}
answer += mn;
}
return answer;
}

0 comments on commit 95179c8

Please sign in to comment.