#include <iostream>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
const int INF =(1<<30);
const int MAX_V = 100;
int V;
//capacity[u][v]=u์์ v๋ก ๋ณด๋ผ ์ ์๋ ์ฉ๋
//flow[u][v]=u์์ v๋ก ํ๋ฌ๊ฐ๋ ์ ๋ (๋ฐ๋ ๋ฐฉํฅ์ธ ๊ฒฝ์ฐ ์์)
int capacity[MAX_V][MAX_V], flow[MAX_V][MAX_V];
//flow[][]๋ฅผ ๊ณ์ฐํ๊ณ ์ด ์ ๋์ ๋ฐํํ๋ค.
int networkflow(int source, int sink){
//flow๋ฅผ 0์ผ๋ก ์ด๊ธฐํ ํ๋ค.
memset(flow, 0, sizeof(flow));
int totalFlow=0;
while(true){
//๋๋น ์ฐ์ ํ์์ผ๋ก ์ฆ๊ฐ ๊ฒฝ๋ก๋ฅผ ์ฐพ๋๋ค.
vector<int> parent(MAX_V, -1);
queue<int>q;
parent[source]=source;
q.push(source);
while(!q.empty() && parent[sink]==-1){
int here=q.front();
q.pop();
for(int there=0; there<V; ++there)
if(capacity[here][there] - flow[here][there] > 0 &&
parent[there]==-1){
q.push(there);
parent[there]=here;
}
}
//์ฆ๊ฐ ๊ฒฝ๋ก๊ฐ ์์ผ๋ฉด ์ข
๋ฃํ๋ค.
if(parent[sink]==-1) break;
//์ฆ๊ฐ ๊ฒฝ๋ก๋ฅผ ํตํด ์ ๋์ ์ผ๋ง๋ ๋ณด๋ผ์ง ๊ฒฐ์ ํ๋ค.
int amount = INF;
for(int p=sink; p!=source; p=parent[p])
amount=min(capacity[parent[p]][p] - flow[parent[p]][p], amount);
//์ฆ๊ฐ ๊ฒฝ๋ก๋ฅผ ํตํด ์ ๋์ ๋ณด๋ธ๋ค.
for(int p=sink; p!=source; p=parent[p]){
flow[parent[p]][p]+=amount;
flow[p][parent[p]]-=amount;
}
totalFlow+=amount;
}
return totalFlow;
}nar789/networkflow
Folders and files
| Name | Name | Last commit date | ||
|---|---|---|---|---|
ย | ย | |||
ย | ย | |||