From d68fcc1054b44462ccabbc33e3a6347052ac98a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=A0=95=EC=9C=A0=ED=99=98?= Date: Sun, 28 Sep 2025 10:04:33 +0900 Subject: [PATCH] =?UTF-8?q?12784=EB=B2=88=20=ED=92=80=EC=9D=B4=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Appendix E/solutions/12784.cpp | 38 ++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/Appendix E/solutions/12784.cpp b/Appendix E/solutions/12784.cpp index 6c991660..dfa9a310 100644 --- a/Appendix E/solutions/12784.cpp +++ b/Appendix E/solutions/12784.cpp @@ -1,11 +1,41 @@ -// Authored by : BaaaaaaaaaaarkingDog +// Authored by : uhwan0723 // Co-authored by : - -// http://boj.kr/**************** +// http://boj.kr/f9af78fd8e5246439d180420f8a57181 #include using namespace std; +const int mn = 1005, md = 25; +int t, n, m; +vector> adj[mn]; // {향하는 정점, 간선의 가중치} 형태로 저장 + +/* +dfs(cur) = 정점 cur을 루트로 하는 서브 트리에서 cur과 리프노드들과의 연결을 끊기 위해 필요한 최소 다이너마이트의 개수. + = cur의 모든 자식 ch에 대해 min(cur-ch 다리 폭파 비용, dfs(cur, ch)) 값의 합 +*/ +int dfs(int par, int cur){ + int res = 0; + for(auto nxt : adj[cur]){ + if(nxt.first == par) continue; + res += min(nxt.second, dfs(cur, nxt.first)); + } + if(cur != 1 && res == 0) res = md; + return res; +} + int main(void){ ios::sync_with_stdio(0); cin.tie(0); - -} \ No newline at end of file + cin >> t; + while(t--){ + cin >> n >> m; + while(m--){ + int u, v, w; + cin >> u >> v >> w; + adj[u].push_back({v, w}); + adj[v].push_back({u, w}); + } + cout << dfs(-1, 1) << '\n'; + for(int i = 1; i <= n; ++i) + adj[i].clear(); + } +}