반응형
문제 출처 :
https://www.acmicpc.net/problem/11408
알고리즘 분석 :
문제 해결에 필요한 사항
1. MCMF
2. 그래프 모델링
가장 MCMF문제 중 기본이 되는 문제이다.
그래프를 다음과 같이 모델링해보자.
S-직원-일-T
1. Source와 직원을 연결한다. 이때 capacity = 1로 둔다. (한명의 직원이 하나의 일만 할 수 있으니)
2. 일과 Sink를 연결한다. 이때 capacity = 1로 둔다. (한명의 직원이 하나의 일만 할 수 있으니)
3. 입력값을 처리한다. 이때 직원과 일사이의 그래프는 입력으로 들어오는 cost를 이용하고 capacity = 1로 둔다.
이제 MCMF를 이용하면 문제를 해결 할 수 있다.
소스 코드 :
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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 | #include <iostream> #include <cstdio> #include <vector> #include <algorithm> #include <queue> using namespace std; const int MAX_V = 900; const int S = MAX_V - 2; const int T = MAX_V - 1; const int WORK = 400; const int INF = 987654321; vector<int> adj[MAX_V]; int c[MAX_V][MAX_V]; int f[MAX_V][MAX_V]; int d[MAX_V][MAX_V]; int main() { int n, m; scanf("%d %d", &n, &m); // S :: 898 T :: 899 직원 :: 1 ~ 400 일 :: 401 ~ 800 // S와 직원을 연결 for (int i = 1; i <= n; i++) { c[S][i] = 1; d[S][i] = 0; d[i][S] = 0; adj[S].push_back(i); adj[i].push_back(S); } // T와 일을 연결 for (int i = 1; i <= m; i++) { c[i + WORK][T] = 1; d[i + WORK][T] = 0; d[T][i + WORK] = 0; adj[i + WORK].push_back(T); adj[T].push_back(i + WORK); } // 입력값 처리 for (int i = 1; i <= n; i++) { int wNum; scanf("%d", &wNum); for (int j = 0; j < wNum; j++) { int workNo, cost; scanf("%d %d", &workNo, &cost); adj[i].push_back(workNo + WORK); adj[workNo + WORK].push_back(i); d[i][workNo + WORK] = cost; d[workNo + WORK][i] = -cost; c[i][workNo + WORK] = 1; } } int result = 0; int cnt = 0; while (1) { int prev[MAX_V], dist[MAX_V]; bool inQ[MAX_V] = { 0 }; queue<int> q; fill(prev, prev + MAX_V, -1); fill(dist, dist + MAX_V, INF); dist[S] = 0; inQ[S] = true; q.push(S); while (!q.empty()) { int here = q.front(); q.pop(); inQ[here] = false; for (int i = 0; i < adj[here].size(); i++) { int next = adj[here][i]; if (c[here][next] - f[here][next] > 0 && dist[next] > dist[here] + d[here][next]) { dist[next] = dist[here] + d[here][next]; prev[next] = here; if (!inQ[next]) { q.push(next); inQ[next] = true; } } } } if (prev[T] == -1) break; int flow = INF; for (int i = T; i != S; i = prev[i]) flow = min(flow, c[prev[i]][i] - f[prev[i]][i]); for (int i = T; i != S; i = prev[i]) { result += flow * d[prev[i]][i]; f[prev[i]][i] += flow; f[i][prev[i]] -= flow; } cnt++; } printf("%d\n%d\n", cnt, result); return 0; } // This source code Copyright belongs to Crocus // If you want to see more? click here >> | Crocus |
반응형
'Applied > 알고리즘 문제풀이' 카테고리의 다른 글
[7154번] Job Postings (0) | 2017.12.05 |
---|---|
[11652번] 카드 (0) | 2017.12.04 |
[1135번] 뉴스 전하기 (0) | 2017.12.02 |
[1080번] 행렬 (0) | 2017.12.01 |
[14626번] ISBN (0) | 2017.11.30 |