반응형
문제 출처 :
https://www.acmicpc.net/problem/11409
알고리즘 분석 :
문제 해결에 필요한 사항
1. MCMF
2. 그래프 모델링
그래프 모델링만 잘 한다면 이 문제는 쉽게 해결할 수 있다.
S-직원-일-T로 그래프를 모델링한다.
1. Source와 직원을 연결한다. 이때 capacity = 1로 준다.
2. 일과 Sink를 연결한다. 이때 capacity = 1로 준다.
3. 마지막으로 각 사람이 할수있는 일과 비용을 정리한다. 이때 cost는 음수로 줘야한다.(최대 월급을 말하고 있기 때문.)
이렇게 그래프를 마무리하고 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 | #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; adj[S].push_back(i); adj[i].push_back(S); } // T와 일을 연결 for (int i = 1; i <= m; i++) { c[i + WORK][T] = 1; 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 }; // SPFA 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 > 알고리즘 문제풀이' 카테고리의 다른 글
[11405번] 책 구매하기 (0) | 2017.12.06 |
---|---|
[3980번] 선발 명단 (0) | 2017.12.06 |
[7154번] Job Postings (0) | 2017.12.05 |
[11652번] 카드 (0) | 2017.12.04 |
[11408번] 열혈강호 5 (0) | 2017.12.04 |