From 4edb6fff9d110fca71ca351ced62228107d46ae4 Mon Sep 17 00:00:00 2001 From: yuyu0830 Date: Tue, 2 Jul 2024 16:23:54 +0900 Subject: [PATCH 1/2] 24/07/02 ACM Craft --- yuyu0830/README.md | 1 + .../1005.cpp" | 66 +++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 "yuyu0830/\354\234\204\354\203\201\354\240\225\353\240\254/1005.cpp" diff --git a/yuyu0830/README.md b/yuyu0830/README.md index e027046..6c2e8a2 100644 --- a/yuyu0830/README.md +++ b/yuyu0830/README.md @@ -15,4 +15,5 @@ | 11차시| 2024.05.08 | 그리디 | [연료 채우기](https://www.acmicpc.net/problem/1826) | - | | 12차시| 2024.05.13 | 해시 | [두 배열의 합](https://www.acmicpc.net/problem/2143) | - | | 13차시| 2024.05.21 | LIS | [가장 긴 증가하는 부분 수열 2](https://www.acmicpc.net/problem/12015) | - | +| 14차시| 2024.07.02 | 위상정렬 | [ACM Craft](https://www.acmicpc.net/problem/1005) | - | --- \ No newline at end of file diff --git "a/yuyu0830/\354\234\204\354\203\201\354\240\225\353\240\254/1005.cpp" "b/yuyu0830/\354\234\204\354\203\201\354\240\225\353\240\254/1005.cpp" new file mode 100644 index 0000000..d87066c --- /dev/null +++ "b/yuyu0830/\354\234\204\354\203\201\354\240\225\353\240\254/1005.cpp" @@ -0,0 +1,66 @@ +#include +#include + +#define fastio ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); + +using namespace std; + +int f() { + int n, m, t; + cin >> n >> m; + + deque visited(n + 1, 0); + vector arr(n + 1, 0), enter(n + 1, 0), rule[n + 1]; + + for (int i = 1; i <= n; i++) + cin >> arr[i]; + + for (int i = 0; i < m; i++) { + int a, b; cin >> a >> b; + rule[a].push_back(b); + enter[b]++; + } + + cin >> t; + + int ret = 0; + + vector tmp[2]; + bool e = false; + + while (true) { + for (int i = 1; i <= n; i++) { + if (!enter[i] && !visited[i]) { + if (i == t) return ret + arr[i]; + + visited[i] = true; + tmp[e].push_back(i); + } + } + + int m = 99999999; + + for (int i : tmp[e]) { m = min(m, arr[i]); } + + while (!tmp[e].empty()) { + int a = tmp[e].back(); + tmp[e].pop_back(); + + arr[a] -= m; + + if (arr[a]) { tmp[!e].push_back(a); } + else { for (int i : rule[a]) { enter[i]--; } } + } + + e = !e; + ret += m; + } +} + +int main() { + fastio + + int testCase; cin >> testCase; + while (testCase--) + printf("%d\n", f()); +} \ No newline at end of file From c1fae0b53c80278c1102fee9be5c64125e24858f Mon Sep 17 00:00:00 2001 From: yuyu0830 Date: Tue, 2 Jul 2024 16:27:36 +0900 Subject: [PATCH 2/2] =?UTF-8?q?=EC=A3=BC=EC=84=9D=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../1005.cpp" | 7 +++++++ 1 file changed, 7 insertions(+) diff --git "a/yuyu0830/\354\234\204\354\203\201\354\240\225\353\240\254/1005.cpp" "b/yuyu0830/\354\234\204\354\203\201\354\240\225\353\240\254/1005.cpp" index d87066c..9de5a52 100644 --- "a/yuyu0830/\354\234\204\354\203\201\354\240\225\353\240\254/1005.cpp" +++ "b/yuyu0830/\354\234\204\354\203\201\354\240\225\353\240\254/1005.cpp" @@ -12,9 +12,11 @@ int f() { deque visited(n + 1, 0); vector arr(n + 1, 0), enter(n + 1, 0), rule[n + 1]; + // 건물 짓는 속도 for (int i = 1; i <= n; i++) cin >> arr[i]; + // 건물 순서 for (int i = 0; i < m; i++) { int a, b; cin >> a >> b; rule[a].push_back(b); @@ -28,7 +30,10 @@ int f() { vector tmp[2]; bool e = false; + // 원하는 건물 지을 수 있을 때까지 반복 while (true) { + // ret 시간에 지을 수 있는 건물들 탐색 및 tmp 큐에 push + // 원하는 건물을 지을 수 있으면 함수 종료 및 값 반환 for (int i = 1; i <= n; i++) { if (!enter[i] && !visited[i]) { if (i == t) return ret + arr[i]; @@ -40,8 +45,10 @@ int f() { int m = 99999999; + // 지금 짓고있는 건물 + 지을 수 있는 건물 중 시간 가장 적게 남은 값 탐색 for (int i : tmp[e]) { m = min(m, arr[i]); } + // tmp 큐에 있는 모든 건물들 m만큼 건설 진행, 완성되면 큐에서 제거 while (!tmp[e].empty()) { int a = tmp[e].back(); tmp[e].pop_back();