-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patha64_q4_path_sum.cpp
More file actions
61 lines (47 loc) · 1.31 KB
/
Copy patha64_q4_path_sum.cpp
File metadata and controls
61 lines (47 loc) · 1.31 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
#include <bits/stdc++.h>
using namespace std;
int n, K[8];
vector <pair <int, int>> graph[20];
bool visited[20];
bool solve(const int &u, const int &tw, const int &K) {
if (tw == K) return true;
if (tw > K) return false;
int maxw = tw;
for (int i = 0; i < n; i++) {
if (visited[i] == false and !graph[i].empty()) maxw += graph[i][0].first;
}
if (maxw < K) return false;
for (auto &[w, v] : graph[u]) {
if (visited[v] == true) continue;
visited[v] = true;
bool ok = solve(v, tw + w, K);
visited[v] = false;
if (ok == true) return true;
}
return false;
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int m;
cin >> n >> m;
for (int i = 0; i < 8; i++) cin >> K[i];
while (m--) {
int a, b, w;
cin >> a >> b >> w;
graph[a].emplace_back(w, b);
graph[b].emplace_back(w, a);
}
for (int i = 0; i < n; i++) sort(graph[i].rbegin(), graph[i].rend());
for (int i = 0; i < 8; i++) {
bool ok = false;
for (int j = 0; j < n; j++) {
visited[j] = true;
ok |= solve(j, 0, K[i]);
visited[j] = false;
if (ok == true) break;
}
if (ok == true) cout << "YES\n";
else cout << "NO\n";
}
return 0;
}