-
Notifications
You must be signed in to change notification settings - Fork 0
/
12784_dfs.cpp
59 lines (58 loc) · 1.1 KB
/
12784_dfs.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// 210608 #12784 ÀÎÇÏ´ÏÄ« °øȱ¹ Gold III
// dfs
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <cstring>
#include <cmath>
#define all(v) (v).begin(), (v).end()
#define press(v) (v).erase(unique(all(v)), (v).end())
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
const int MAX = 1001;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
int N, M;
vector<pi> v[MAX];
bool visit[MAX];
int dfs(int cur) {
visit[cur] = true;
int ret = 0;
bool flag = false;
for (auto i : v[cur]) {
int cost = i.first;
if (!visit[i.second]) {
ret += min(cost, dfs(i.second));
flag = true;
}
}
return flag ? ret : INF;
}
int main() {
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(false);
int t;
cin >> t;
while (t--) {
for (auto& i : v) {
i.clear();
}
fill(visit, visit + MAX, false);
cin >> N >> M;
if (N == 1) {
cout << 0 << "\n";
continue;
}
for (int i = 0; i < M; i++) {
int a, b, cost;
cin >> a >> b >> cost;
v[a].push_back({ cost, b });
v[b].push_back({ cost, a });
}
cout << dfs(1) << "\n";
}
}