forked from sitz/UVa-Online-Judge
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path10807.cpp
154 lines (143 loc) · 2.02 KB
/
10807.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
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <bits/stdc++.h>
using namespace std;
#define INF 999999999
struct edge {int a, b, c; bool v;} e[25];
int p[11], p2[11], n, m, min_;
int cmp(const void *a, const void *b)
{
edge *c = (edge *)a, *d = (edge *)b;
return c->c - d->c;
}
int find(int x)
{
while (p[x] > 0)
{
x = p[x];
}
return x;
}
int find2(int x)
{
while (p2[x] > 0)
{
x = p2[x];
}
return x;
}
void union_(int x, int y)
{
if (p[x] > p[y])
{
p[y] += p[x];
p[x] = y;
}
else
{
p[x] += p[y];
p[y] = x;
}
}
void union_2(int x, int y)
{
if (p2[x] > p2[y])
{
p2[y] += p2[x];
p2[x] = y;
}
else
{
p2[x] += p2[y];
p2[y] = x;
}
}
int mst(int bound)
{
for (int i = 1; i <= n; i++)
{
p2[i] = -1;
}
int count = 0, sum = 0;
for (int i = 0; i < m && count < n - 1; i++)
{
if (e[i].v)
{
continue;
}
int pa = find2(e[i].a), pb = find2(e[i].b);
if (pa != pb)
{
union_2(pa, pb);
count++;
sum += e[i].c;
if (sum >= bound)
{
return INF;
}
}
}
return (count == n - 1) ? sum : INF;
}
void dfs(int choose, int from, int weight)
{
if (choose == n - 1)
{
if (weight < min_)
{
int second = mst(min_ - weight);
if (weight + second < min_)
{
min_ = weight + second;
}
}
}
else if (choose + m - from >= n - 1)
{
if (weight + e[from].c * (n - 1 - m + from) >= min_)
{
return;
}
int tpa = find(e[from].a), tpb = find(e[from].b);
if (tpa != tpb)
{
int a = p[tpa], b = p[tpb];
union_(tpa, tpb);
e[from].v = true;
dfs(choose + 1, from + 1, weight + e[from].c);
p[tpa] = a;
p[tpb] = b;
e[from].v = false;
}
if (weight < min_)
{
dfs(choose, from + 1, weight);
}
}
}
int main()
{
while (scanf("%d", &n), n)
{
scanf("%d", &m);
for (int i = 0; i < m; i++)
{
scanf("%d %d %d", &e[i].a, &e[i].b, &e[i].c);
e[i].v = false;
}
qsort(e, m, sizeof(edge), cmp);
min_ = INF;
for (int i = 1; i <= n; i++)
{
p[i] = -1;
}
dfs(0, 0, 0);
if (min_ != INF)
{
printf("%d\n", min_);
}
else
{
puts("No way!");
}
}
return 0;
}