-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10462.cpp
More file actions
87 lines (83 loc) · 2.06 KB
/
10462.cpp
File metadata and controls
87 lines (83 loc) · 2.06 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/**
* ____
* ____ ___ ____ ________ __/ __/
* / __ `__ \/ __ `/ ___/ / / / /_
* / / / / / / /_/ / / / /_/ / __/
* /_/ /_/ /_/\__,_/_/ \__,_/_/
*
* @link : https://the-redback.com
*/
#include <bits/stdc++.h>
using namespace std;
#define inf 1000000000
#define mem(a, b) memset(a, b, sizeof(a))
#define NN 100
struct edge {
int u, v, w, flag;
};
vector<edge> e;
int pr[NN + 7];
int Flag;
bool comp(edge n, edge m) { return n.w < m.w; }
int root(int n) {
if (n == pr[n]) return n;
return root(pr[n]);
}
int mst(int n) {
int i, j, k;
for (i = 0; i <= n; i++) pr[i] = i;
int count = 0, cost = 0;
for (i = 0; i < e.size() && count < n - 1; i++) {
int u = root(e[i].u);
int v = root(e[i].v);
if (u != v && e[i].flag) {
pr[u] = v;
count++;
cost += e[i].w;
if (!Flag)
e[i].flag = 2;
else
e[i].flag = max(e[i].flag, 1);
}
}
Flag = 1;
if (count != n - 1) return inf;
return cost;
}
int main() {
// freopen("C:\\Users\\Maruf Tuhin\\Desktop\\in.txt","r",stdin);
// ios_base::sync_with_stdio(false);
int i, j, k, l, n, r, c, u, v, w;
int tc, t = 1, m;
edge ed;
scanf("%d", &tc);
while (tc--) {
scanf("%d%d", &n, &r);
e.clear();
while (r--) {
scanf("%d%d%d", &ed.u, &ed.v, &ed.w);
ed.flag = 1;
e.push_back(ed);
}
Flag = 0;
sort(e.begin(), e.end(), comp);
int sum = mst(n);
if (sum == inf) {
printf("Case #%d : No way\n", t++);
continue;
}
r = inf;
for (i = 0; i < e.size(); i++) {
if (e[i].flag == 2) {
e[i].flag = 0;
r = min(r, mst(n));
e[i].flag = 2;
}
}
if (r == inf)
printf("Case #%d : No second way\n", t++);
else
printf("Case #%d : %d\n", t++, r);
}
return 0;
}