// 度度熊的王国战略2.cpp 运行/限制:140ms/2000ms #include #include #include #include using namespace std; int n, m, fa[3005], sum[3005]; int get(int x) { return fa[x] = fa[x] == x ? x : get(fa[x]); } void merge(int x, int y) { int a = get(x); int b = get(y); if (a != b) { fa[b] = a; } } void init() { for (int i = 1; i <= n; i++) { fa[i] = i; } } int main(){ int a, b, c, cnt; while (scanf("%d%d", &n, &m) != EOF) { init(); cnt = n - 1; memset(sum, 0, sizeof(sum)); for (int i = 0; i < m; i++) { scanf("%d%d%d", &a, &b, &c); if (a == b) continue; sum[a] += c; sum[b] += c; int x = get(a); int y = get(b); if (x != y) { merge(x, y); cnt--; } } if (cnt) { printf("0\n"); } else{ sort(sum + 1, sum + n + 1); printf("%d\n", sum[1]); } } return 0; }