-
Notifications
You must be signed in to change notification settings - Fork 0
/
manel.cpp
94 lines (70 loc) · 1.89 KB
/
manel.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
#include <iostream>
#include <vector>
int best;
void solve(int **cost, bool *active, int *link, int n, int k, int v, int c){
if(c >= best)
return;
if(v == n){
best = c;
return;
}
for(int i = 0; i < n; ++i){
if(active[i] && link[i] < k){
for(int j = 0; j < n; ++j){
if(!active[j]){
if(cost[i][j] >= 0){
active[j] = true;
link[i]++;
link[j]++;
solve(cost, active, link, n, k, v+1, c + cost[i][j]);
active[j] = false;
link[j]--;
link[i]--;
}
}
}
}
}
}
int main() {
int n, m, k;
while(std::cin >> n >> m >> k){
best = INT32_MAX;
int **cost = new int*[n];
for(int i = 0; i < n; ++i)
cost[i] = new int[n];
bool *active = new bool[n];
int *link = new int[n];
for(int i = 0; i < n; ++i){
active[i] = false;
}
active[0] = true;
for(int i = 0; i < n; ++i){
link[i] = 0;
}
for(int i = 0; i < n; ++i){
for(int j = 0; j < n; ++j){
cost[i][j] = -1;
}
}
for(int i = 0; i < m; ++i){
int x, y;
std::cin >> x >> y;
std::cin >> cost[x-1][y-1];
cost[y-1][x-1] = cost[x-1][y-1];
}
solve(cost, active, link, n, k, 1, 0);
if(best == INT32_MAX){
std::cout << "NO WAY!" << std::endl;
}
else{
std::cout << best << std::endl;
}
delete[] active;
for(int i = 0; i < n; ++i)
delete[] cost[i];
delete[] cost;
delete[] link;
}
return 0;
}