-
[2637] 장난감 조립BOJ 2022. 3. 23. 16:46
https://www.acmicpc.net/problem/2637
2637번: 장난감 조립
첫째 줄에는 자연수 N(3 ≤ N ≤ 100)이 주어지는데, 1부터 N-1까지는 기본 부품이나 중간 부품의 번호를 나타내고, N은 완제품의 번호를 나타낸다. 그리고 그 다음 줄에는 자연수 M(3 ≤ M ≤ 100)이 주
www.acmicpc.net
"[1516] 게임 개발"과 비슷하게, 위상정렬 하면서 방문하게 되는 노드에 dp를 적용한다.
간선의 방향을 뒤집고, 항상 n부터 출발하도록 구현할 수 있다.
#include <bits/stdc++.h>using namespace std;#ifdef ONLINE_JUDGEconstexpr bool local = false;#elseconstexpr bool local = true;#endifusing ll = long long;using pi = pair<ll, ll>;int n, m, d[105], cost[105];typedef struct {int idx, cost;} state;vector<state> adj[105];queue<int> q;bool chk[105];int main(void) {if (!local) ios_base::sync_with_stdio(0), cin.tie(0);cin >> n >> m;int i;memset(chk, true, sizeof(chk));for (i = 0; i < m; i++) {int A, B, C;cin >> A >> B >> C;adj[A].push_back({B, C});d[B]++;chk[A] = false;}q.push(n);cost[n] = 1;while (!q.empty()) {int cur = q.front();q.pop();for (auto& nxt : adj[cur]) {d[nxt.idx]--;cost[nxt.idx] = cost[nxt.idx] + (cost[cur] * nxt.cost);if (d[nxt.idx] == 0) q.push(nxt.idx);}}for (i = 1; i < n; i++)if (chk[i]) cout << i << ' ' << cost[i] << '\n';return 0;}
'BOJ' 카테고리의 다른 글
[5637] 가장 긴 단어 (0) 2022.03.25 [19236] 청소년 상어 (0) 2022.03.23 [1613] 역사 (0) 2022.03.23 [14567] 선수과목 (Prerequisite) (0) 2022.03.22 [18268] Cow Gymnastics (0) 2022.03.22