티스토리 뷰
백준 소스코드 [C++] 6086 최대 유량
#include <iostream>
#include <algorithm>
#include <queue>
#include <string.h>
#include <limits.h>
#include <vector>
#include <math.h>
#include <stack>
#include <bitset>
#include <string>
#include <set>
#include <map>
#include <unordered_map>
#include <sstream>
#define all(v) v.begin(), v.end()
#define pii pair<int, int>
#define make_unique(v) v.erase(unique(v.begin(), v.end()), v.end())
typedef long long ll;
using namespace std;
int E, w,res=0;
char u, v;
int cap[52][52], flow[52][52],parent[52];
vector<int> path[52];
int charToInt(char c) {
if (c <= 'Z') return c - 'A';
return c - 'a' + 26;
}
void edmond() {
int sink = charToInt('Z');
int src = charToInt('A');
while (true) {
memset(parent, -1, sizeof(parent));
queue<int> q;
q.push(src);
while (q.empty() != true) {
int here = q.front(); q.pop();
if (here == sink) break;
for (int i = 0;i < (int)path[here].size();i++) {
int next = path[here][i];
if (parent[next] == -1 && flow[here][next] < cap[here][next]) {
parent[next] = here;
q.push(next);
}
}
}
if (parent[sink] == -1) break;
int amount = 987654321;
for (int vertex = sink;vertex != src;vertex = parent[vertex])
amount = min(cap[parent[vertex]][vertex] - flow[parent[vertex]][vertex], amount);
for (int vertex = sink;vertex != src;vertex = parent[vertex]) {
flow[parent[vertex]][vertex] += amount;
flow[vertex][parent[vertex]] -= amount;
}
res += amount;
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> E;
for (int i = 0;i < E;i++) {
cin >> u >> v >> w;
int U = charToInt(u), V = charToInt(v);
cap[U][V] += w; cap[V][U] += w;
path[U].push_back(V), path[V].push_back(U);
}
edmond();
cout << res;
return 0;
}
'백준' 카테고리의 다른 글
백준 소스코드 [C++] 10423 전기가 부족해 (0) | 2021.03.27 |
---|---|
백준 소스코드 [C++] 10840 구간 성분 (0) | 2021.03.13 |
백준 소스코드 [C++] 10775 공항 (0) | 2021.03.10 |
백준 소스코드 [C++] 1436 영화감독 숌 (0) | 2021.03.09 |
백준 소스코드 [C++] 수열과 쿼리 26 (0) | 2021.03.07 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- MeTal
- 에드몬드 카프 알고리즘
- 최단경로문제
- WWDC21
- rxswift
- mach-o
- 벨만포드 알고리즘
- Testable
- 컴퓨터 추상화
- test coverage
- 다익스트라 시간복잡도
- 네트워크 플로우
- CompositionalLayout
- State Restoration
- 부스트캠프 6기
- CPU와 Memory
- 포드 풀커슨 알고리즘
- observeOn
- 최단경로 문제
- WWDC19
- 강한 순환 참조
- 코딩대회
- IOS
- 벨만포드 시간복잡도
- 최단경로 알고리즘
- HIG
- 네트워크 유량
- WWDC17
- WWDC16
- 최대 매칭
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함