PS
BOJ 10976 : 피난
lickelon
2024. 11. 9. 02:14
- 문제 링크 : boj.kr/10976
- 난이도 : P4
- 태그 : 최대 유량
코드
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
#define INF 0x7FFFFFFF
using namespace std;
using ll = long long;
using ld = long double;
using pii = pair<int,int>;
using pll = pair<ll, ll>;
class Flow {
vector<vector<int>> edge;
vector<vector<int>> f, c;
int n;
public:
Flow(int n) {
this->n = n;
edge.resize(n);
f = vector<vector<int>>(n, vector<int>(n));
c = vector<vector<int>>(n, vector<int>(n));
}
int flow(int start, int end) {
init();
int ans = 0;
while(true) {
//BFS
vector<int> parent(n, -1);
queue<int> _q;
parent[start] = start;
_q.push(start);
while(!_q.empty() && parent[end] == -1) {
int curr = _q.front(); _q.pop();
for(auto u : edge[curr]) {
if(c[curr][u] - f[curr][u] > 0 && parent[u] == -1) {
_q.push(u);
parent[u] = curr;
if(u == end) break;
}
}
}
if(parent[end] == -1) break;
//check amount
int amount = INF;
int curr = end;
while(curr != start) {
int before = parent[curr];
amount = min(amount, c[before][curr] - f[before][curr]);
curr = before;
}
//set
curr = end;
while(curr != start) {
int before = parent[curr];
f[before][curr] += amount;
f[curr][before] -= amount;
curr = before;
}
ans += amount;
}
return ans;
}
void add_edge(int x, int y, int c, bool b = 0) {
edge[x].push_back(y);
edge[y].push_back(x);
this->c[x][y] = c;
if(b) this->c[x][y] = c; //양방향 그래프
}
private:
void init() {
for(auto &u : f) {
for(auto &v : u) {
v = 0;
}
}
}
};
int main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int T;
cin >> T;
while(T--) {
int n, m;
cin >> n >> m;
Flow f(n+1);
for(int i = 0; i < m; i++) {
int a, b;
cin >> a >> b;
if(a == 1 || b == n) f.add_edge(a, b, 1);
else f.add_edge(a, b, INF);
}
cout << f.flow(1, n) << "\n";
}
return 0;
}
풀이
문제에서 말하는 대로 그래프를 구성하고, 최대 유량을 구하면 된다.
728x90