From 93ab469de05263ae3514ad34be0d311534470240 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=B5=9C=EB=AC=B8=ED=98=95?= <74577714+alirz-pixel@users.noreply.github.com> Date: Mon, 26 Jan 2026 22:11:07 +0900 Subject: [PATCH] =?UTF-8?q?260126=20:=20[BOJ=2012784]=20=EC=9D=B8=ED=95=98?= =?UTF-8?q?=EB=8B=88=EC=B9=B4=20=EA=B3=B5=ED=99=94=EA=B5=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- _munhyeong/12784.cpp | 57 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 _munhyeong/12784.cpp diff --git a/_munhyeong/12784.cpp b/_munhyeong/12784.cpp new file mode 100644 index 00000000..655abf44 --- /dev/null +++ b/_munhyeong/12784.cpp @@ -0,0 +1,57 @@ +#include +#include + +using namespace std; + +struct Info { + int to; + int weight; +}; + +int dfs(vector &visited, vector> &edges, int start) { + visited[start] = true; + + if (start != 0 && edges[start].size() == 1) + return edges[start][0].weight; + + int need = 0; + + for (Info edge : edges[start]) { + if (visited[edge.to]) + continue; + + need += min(edge.weight, dfs(visited, edges, edge.to)); + } + + //cout << start << " " << need << "\n"; + return need; +} + +int solve() { + int N, M; + cin >> N >> M; + + vector> edges(N); + vector visited(N); + for (int i = 0; i < M; i++) { + int from, to, weight; + cin >> from >> to >> weight; + + from--; to--; + edges[from].push_back({ to, weight }); + edges[to].push_back({ from, weight }); + } + + return dfs(visited, edges, 0); +} + +int main() { + int T; + cin >> T; + + while (T--) { + cout << solve() << "\n"; + } + + return 0; +}