1 #include <cstdio>
2 #include <cstdlib>
3 #include <cstring>
4 #include <string>
5 #define MAX 102
6 #define inf 0x3f3f3f3f
7 int flag[MAX];
8 int cost[MAX][MAX];
9 int lowCost[MAX];
10
11 int main(int argc, char const *argv[])
12 {
13 int n,m;
14 scanf("%d",&n);
15 while(n != 0) {
16 int count = 0;
17 m = n * (n-1)/2;
18 for(int i = 1; i <= n; i++) {
19 flag[i] = 0;
20 for(int j = 1; j <= n; j++) {
21 cost[i][j] = inf;
22 }
23 }
24 for(int i = 0; i < m; i++) {
25 int a,b,c,d;
26 scanf("%d %d %d %d",&a,&b,&c,&d);
27 if(d == 0) {
28 cost[a][b]= cost[b][a] = c;
29 }
30 else {
31 cost[a][b]= cost[b][a] = 0;
32 }
33 }
34
35 int sumCost = 0;
36 for(int i = 1; i <= n; i++) {
37 lowCost[i] = cost[1][i];
38 }
39 flag[1] = 1;
40
41 for(int i = 1; i <= n; i++) {
42 int min = inf;
43 int v = -1;
44 for(int i = 1; i <= n; i++) {
45 if(flag[i] == 0 && lowCost[i] < min) {
46 min = lowCost[i];
47 v = i;
48 }
49 }
50 flag[v] = 1;
51 sumCost = sumCost + lowCost[v];
52
53 for(int i = 1; i <= n; i++) {
54 if(cost[v][i] < lowCost[i]) {
55 lowCost[i] = cost[v][i];
56 }
57 }
58 }
59
60 printf("%d
",sumCost);
61
62 scanf("%d",&n);
63 }
64
65 return 0;
66 }