1 #include <cstdio>
2 #include <cstdlib>
3 #include <algorithm>
4 #include <cmath>
5 using namespace std;
6
7 #define MAXN 10005
8 int fa[MAXN];
9 struct node
10 {
11 int from, to, len;
12 }arr[MAXN];
13
14 bool cmp(node a, node b)
15 {
16 return a.len < b.len;
17 }
18
19 int m;
20
21 int find(int x)
22 {
23 if (x == fa[x])
24 return x;
25 else
26 return fa[x] = find(fa[x]);
27 }
28
29 int main()
30 {
31 int n;
32 while (scanf("%d",&n)&&n)
33 {
34 for (int i = 0; i <= n; i++)
35 fa[i] = i;
36 m = 0;
37 int s = n*(n - 1) / 2;
38 for (int i = 0; i < s; i++)
39 {
40 int a, b, c, d;
41 scanf("%d%d%d%d", &a, &b, &c, &d);
42 if (d)
43 {
44 int x = find(a);
45 int y = find(b);
46 if (x != y)
47 fa[y] = x;
48 }
49 else
50 {
51 arr[m].from = a;
52 arr[m].to = b;
53 arr[m].len = c;
54 m++;
55 }
56 }
57
58 sort(arr, arr + m, cmp);
59 int ans = 0;
60 for (int i = 0; i < m; i++)
61 {
62 int a = find(arr[i].from);
63 int b = find(arr[i].to);
64 if (a != b)
65 {
66 ans += arr[i].len;
67 fa[b] = a;
68 }
69 }
70 printf("%d\n", ans);
71 }
72 //system("pause");
73 return 0;
74 }