diff --git a/_sungyoon/12784.cpp b/_sungyoon/12784.cpp new file mode 100644 index 00000000..b21e8e5a --- /dev/null +++ b/_sungyoon/12784.cpp @@ -0,0 +1,74 @@ +#include +using namespace std; +typedef pair pii; +typedef long long ll; +#define endl "\n" + +struct Tree { + int Node, left, right; +}; + +const int INF = 1e9; +const int MAX = 1001; +const int MOD = 1e9 + 7; +int dx[] = {0, 0, 1, -1}; +int dy[] = {1, -1, 0, 0}; +int T, N, M; +int dp[MAX]; + +int func(int node, int par, vector> &v) { + int &ret = dp[node]; + + if(ret != -1) return ret; + + ret = 0; + + bool isleaf = false; + + for(auto &it : v[node]) { + int nextnode = it.first; + int cost = it.second; + + if(par != nextnode) { + isleaf = true; + ret += min(func(nextnode, node, v), cost); + } + } + + return ((isleaf)? ret : INF); +} + +void solve(vector> &v) { + if(N == 1) { + cout << 0 << endl; + } + else cout << func(1, 0, v) << endl; +} + +void input() { + cin >> T; + + while(T--) { + cin >> N >> M; + + vector> v(N+1); + + memset(dp, -1, sizeof(dp)); + + for(int i = 0; i < M; i++) { + int a, b, c; + cin >> a >> b >> c; + v[a].push_back({b, c}); + v[b].push_back({a, c}); + } + + solve(v); + } +} + +int main() { + ios::sync_with_stdio(0); + cin.tie(0), cout.tie(0); + + input(); +}