#include <cstdio>
#include <vector>
#include <utility>
#include <algorithm>
using namespace std;
int n, m, dp[20][20971520];
vector<int> tree[200006];
vector<pair<int, int>> adj[200006];
int f(int x, int y) {
if (dp[x][y] + 1) return dp[x][y];
if (!y) return dp[x][y] = 0;
dp[x][y] = (int)1e9;
for (auto &i: tree[x]) dp[x][y] = min(dp[x][y], 1 + f(i, y & ~(1 << i)));
for (auto &i: adj[x]) dp[x][y] = min(dp[x][y], i.second + f(i.first, y & ~(1 << i.first)));
return dp[x][y];
}
int lt[5006][5006], dist[200006];
void dfs(int x, int y, int dist = 0, int prev = -1) {
lt[y][x] = dist;
for (auto &i: tree[x]) if (i != prev) dfs(i, y, dist + 1, x);
}
int dfs_dist(int x, int prev = -1) {
int ret = x;
for (auto &i: tree[x]) if (i != prev) {
dist[i] = 1 + dist[x];
int t = dfs_dist(i, x);
if (dist[t] > dist[ret]) ret = t;
}
return ret;
}
int main() {
scanf("%d%d", &n, &m);
for (int i = 0; i < n; i++) for (int j = 0; j < 1 << n; j++) dp[i][j] = -1;
while (m--) {
int x, y, w;
scanf("%d%d%d", &x, &y, &w);
if (w == 1) {
tree[x].push_back(y);
tree[y].push_back(x);
} else {
adj[x].push_back({ y, w });
adj[y].push_back({ x, w });
}
}
if (n <= 200000) {
int res = (int)1e9;
for (int i = 0; i < n; i++) res = min(res, f(i, (1 << n) - 1 ^ 1 << i));
printf("%d", res);
return 0;
}
for (int i = 0; i < n; i++) dfs(i, i);
int t = dfs_dist(dist[0] = 0);
dist[t] = 0;
int zero = 2 * (n - 1) - dist[dfs_dist(t)], one = (int)1e9, two = (int)1e9, three = (int)1e9;
for (int i = 0; i < n; i++) for (auto &j: adj[i]) if (i < j.first) one = min(one, j.second + 2 * (n - 1) - lt[i][j.first]);
printf("%d", min({ zero, one, two, three }));
}
Compilation message
Main.cpp: In function 'int main()':
Main.cpp:53:66: warning: suggest parentheses around arithmetic in operand of '^' [-Wparentheses]
53 | for (int i = 0; i < n; i++) res = min(res, f(i, (1 << n) - 1 ^ 1 << i));
| ~~~~~~~~~^~~
Main.cpp:38:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
38 | scanf("%d%d", &n, &m);
| ~~~~~^~~~~~~~~~~~~~~~
Main.cpp:42:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
42 | scanf("%d%d%d", &x, &y, &w);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9676 KB |
Output is correct |
2 |
Correct |
5 ms |
9652 KB |
Output is correct |
3 |
Correct |
6 ms |
9732 KB |
Output is correct |
4 |
Correct |
5 ms |
9728 KB |
Output is correct |
5 |
Correct |
5 ms |
9676 KB |
Output is correct |
6 |
Correct |
5 ms |
9676 KB |
Output is correct |
7 |
Correct |
5 ms |
9676 KB |
Output is correct |
8 |
Correct |
5 ms |
9676 KB |
Output is correct |
9 |
Correct |
5 ms |
9676 KB |
Output is correct |
10 |
Correct |
5 ms |
9712 KB |
Output is correct |
11 |
Correct |
6 ms |
9676 KB |
Output is correct |
12 |
Correct |
5 ms |
9676 KB |
Output is correct |
13 |
Correct |
5 ms |
9688 KB |
Output is correct |
14 |
Correct |
5 ms |
9676 KB |
Output is correct |
15 |
Correct |
5 ms |
9612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9676 KB |
Output is correct |
2 |
Correct |
5 ms |
9652 KB |
Output is correct |
3 |
Correct |
6 ms |
9732 KB |
Output is correct |
4 |
Correct |
5 ms |
9728 KB |
Output is correct |
5 |
Correct |
5 ms |
9676 KB |
Output is correct |
6 |
Correct |
5 ms |
9676 KB |
Output is correct |
7 |
Correct |
5 ms |
9676 KB |
Output is correct |
8 |
Correct |
5 ms |
9676 KB |
Output is correct |
9 |
Correct |
5 ms |
9676 KB |
Output is correct |
10 |
Correct |
5 ms |
9712 KB |
Output is correct |
11 |
Correct |
6 ms |
9676 KB |
Output is correct |
12 |
Correct |
5 ms |
9676 KB |
Output is correct |
13 |
Correct |
5 ms |
9688 KB |
Output is correct |
14 |
Correct |
5 ms |
9676 KB |
Output is correct |
15 |
Correct |
5 ms |
9612 KB |
Output is correct |
16 |
Correct |
6 ms |
9676 KB |
Output is correct |
17 |
Correct |
5 ms |
9676 KB |
Output is correct |
18 |
Correct |
6 ms |
9676 KB |
Output is correct |
19 |
Correct |
6 ms |
9932 KB |
Output is correct |
20 |
Correct |
8 ms |
11724 KB |
Output is correct |
21 |
Correct |
6 ms |
11628 KB |
Output is correct |
22 |
Correct |
174 ms |
91880 KB |
Output is correct |
23 |
Correct |
59 ms |
48808 KB |
Output is correct |
24 |
Correct |
40 ms |
48760 KB |
Output is correct |
25 |
Correct |
37 ms |
28260 KB |
Output is correct |
26 |
Correct |
59 ms |
28204 KB |
Output is correct |
27 |
Correct |
313 ms |
91968 KB |
Output is correct |
28 |
Correct |
20 ms |
28260 KB |
Output is correct |
29 |
Correct |
31 ms |
28160 KB |
Output is correct |
30 |
Incorrect |
24 ms |
28240 KB |
Output isn't correct |
31 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
20 ms |
19516 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9676 KB |
Output is correct |
2 |
Correct |
5 ms |
9652 KB |
Output is correct |
3 |
Correct |
6 ms |
9732 KB |
Output is correct |
4 |
Correct |
5 ms |
9728 KB |
Output is correct |
5 |
Correct |
5 ms |
9676 KB |
Output is correct |
6 |
Correct |
5 ms |
9676 KB |
Output is correct |
7 |
Correct |
5 ms |
9676 KB |
Output is correct |
8 |
Correct |
5 ms |
9676 KB |
Output is correct |
9 |
Correct |
5 ms |
9676 KB |
Output is correct |
10 |
Correct |
5 ms |
9712 KB |
Output is correct |
11 |
Correct |
6 ms |
9676 KB |
Output is correct |
12 |
Correct |
5 ms |
9676 KB |
Output is correct |
13 |
Correct |
5 ms |
9688 KB |
Output is correct |
14 |
Correct |
5 ms |
9676 KB |
Output is correct |
15 |
Correct |
5 ms |
9612 KB |
Output is correct |
16 |
Correct |
6 ms |
9676 KB |
Output is correct |
17 |
Correct |
5 ms |
9676 KB |
Output is correct |
18 |
Correct |
6 ms |
9676 KB |
Output is correct |
19 |
Correct |
6 ms |
9932 KB |
Output is correct |
20 |
Correct |
8 ms |
11724 KB |
Output is correct |
21 |
Correct |
6 ms |
11628 KB |
Output is correct |
22 |
Correct |
174 ms |
91880 KB |
Output is correct |
23 |
Correct |
59 ms |
48808 KB |
Output is correct |
24 |
Correct |
40 ms |
48760 KB |
Output is correct |
25 |
Correct |
37 ms |
28260 KB |
Output is correct |
26 |
Correct |
59 ms |
28204 KB |
Output is correct |
27 |
Correct |
313 ms |
91968 KB |
Output is correct |
28 |
Correct |
20 ms |
28260 KB |
Output is correct |
29 |
Correct |
31 ms |
28160 KB |
Output is correct |
30 |
Incorrect |
24 ms |
28240 KB |
Output isn't correct |
31 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9676 KB |
Output is correct |
2 |
Correct |
5 ms |
9652 KB |
Output is correct |
3 |
Correct |
6 ms |
9732 KB |
Output is correct |
4 |
Correct |
5 ms |
9728 KB |
Output is correct |
5 |
Correct |
5 ms |
9676 KB |
Output is correct |
6 |
Correct |
5 ms |
9676 KB |
Output is correct |
7 |
Correct |
5 ms |
9676 KB |
Output is correct |
8 |
Correct |
5 ms |
9676 KB |
Output is correct |
9 |
Correct |
5 ms |
9676 KB |
Output is correct |
10 |
Correct |
5 ms |
9712 KB |
Output is correct |
11 |
Correct |
6 ms |
9676 KB |
Output is correct |
12 |
Correct |
5 ms |
9676 KB |
Output is correct |
13 |
Correct |
5 ms |
9688 KB |
Output is correct |
14 |
Correct |
5 ms |
9676 KB |
Output is correct |
15 |
Correct |
5 ms |
9612 KB |
Output is correct |
16 |
Correct |
6 ms |
9676 KB |
Output is correct |
17 |
Correct |
5 ms |
9676 KB |
Output is correct |
18 |
Correct |
6 ms |
9676 KB |
Output is correct |
19 |
Correct |
6 ms |
9932 KB |
Output is correct |
20 |
Correct |
8 ms |
11724 KB |
Output is correct |
21 |
Correct |
6 ms |
11628 KB |
Output is correct |
22 |
Correct |
174 ms |
91880 KB |
Output is correct |
23 |
Correct |
59 ms |
48808 KB |
Output is correct |
24 |
Correct |
40 ms |
48760 KB |
Output is correct |
25 |
Correct |
37 ms |
28260 KB |
Output is correct |
26 |
Correct |
59 ms |
28204 KB |
Output is correct |
27 |
Correct |
313 ms |
91968 KB |
Output is correct |
28 |
Correct |
20 ms |
28260 KB |
Output is correct |
29 |
Correct |
31 ms |
28160 KB |
Output is correct |
30 |
Incorrect |
24 ms |
28240 KB |
Output isn't correct |
31 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9676 KB |
Output is correct |
2 |
Correct |
5 ms |
9652 KB |
Output is correct |
3 |
Correct |
6 ms |
9732 KB |
Output is correct |
4 |
Correct |
5 ms |
9728 KB |
Output is correct |
5 |
Correct |
5 ms |
9676 KB |
Output is correct |
6 |
Correct |
5 ms |
9676 KB |
Output is correct |
7 |
Correct |
5 ms |
9676 KB |
Output is correct |
8 |
Correct |
5 ms |
9676 KB |
Output is correct |
9 |
Correct |
5 ms |
9676 KB |
Output is correct |
10 |
Correct |
5 ms |
9712 KB |
Output is correct |
11 |
Correct |
6 ms |
9676 KB |
Output is correct |
12 |
Correct |
5 ms |
9676 KB |
Output is correct |
13 |
Correct |
5 ms |
9688 KB |
Output is correct |
14 |
Correct |
5 ms |
9676 KB |
Output is correct |
15 |
Correct |
5 ms |
9612 KB |
Output is correct |
16 |
Correct |
6 ms |
9676 KB |
Output is correct |
17 |
Correct |
5 ms |
9676 KB |
Output is correct |
18 |
Correct |
6 ms |
9676 KB |
Output is correct |
19 |
Correct |
6 ms |
9932 KB |
Output is correct |
20 |
Correct |
8 ms |
11724 KB |
Output is correct |
21 |
Correct |
6 ms |
11628 KB |
Output is correct |
22 |
Correct |
174 ms |
91880 KB |
Output is correct |
23 |
Correct |
59 ms |
48808 KB |
Output is correct |
24 |
Correct |
40 ms |
48760 KB |
Output is correct |
25 |
Correct |
37 ms |
28260 KB |
Output is correct |
26 |
Correct |
59 ms |
28204 KB |
Output is correct |
27 |
Correct |
313 ms |
91968 KB |
Output is correct |
28 |
Correct |
20 ms |
28260 KB |
Output is correct |
29 |
Correct |
31 ms |
28160 KB |
Output is correct |
30 |
Incorrect |
24 ms |
28240 KB |
Output isn't correct |
31 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9676 KB |
Output is correct |
2 |
Correct |
5 ms |
9652 KB |
Output is correct |
3 |
Correct |
6 ms |
9732 KB |
Output is correct |
4 |
Correct |
5 ms |
9728 KB |
Output is correct |
5 |
Correct |
5 ms |
9676 KB |
Output is correct |
6 |
Correct |
5 ms |
9676 KB |
Output is correct |
7 |
Correct |
5 ms |
9676 KB |
Output is correct |
8 |
Correct |
5 ms |
9676 KB |
Output is correct |
9 |
Correct |
5 ms |
9676 KB |
Output is correct |
10 |
Correct |
5 ms |
9712 KB |
Output is correct |
11 |
Correct |
6 ms |
9676 KB |
Output is correct |
12 |
Correct |
5 ms |
9676 KB |
Output is correct |
13 |
Correct |
5 ms |
9688 KB |
Output is correct |
14 |
Correct |
5 ms |
9676 KB |
Output is correct |
15 |
Correct |
5 ms |
9612 KB |
Output is correct |
16 |
Correct |
6 ms |
9676 KB |
Output is correct |
17 |
Correct |
5 ms |
9676 KB |
Output is correct |
18 |
Correct |
6 ms |
9676 KB |
Output is correct |
19 |
Correct |
6 ms |
9932 KB |
Output is correct |
20 |
Correct |
8 ms |
11724 KB |
Output is correct |
21 |
Correct |
6 ms |
11628 KB |
Output is correct |
22 |
Correct |
174 ms |
91880 KB |
Output is correct |
23 |
Correct |
59 ms |
48808 KB |
Output is correct |
24 |
Correct |
40 ms |
48760 KB |
Output is correct |
25 |
Correct |
37 ms |
28260 KB |
Output is correct |
26 |
Correct |
59 ms |
28204 KB |
Output is correct |
27 |
Correct |
313 ms |
91968 KB |
Output is correct |
28 |
Correct |
20 ms |
28260 KB |
Output is correct |
29 |
Correct |
31 ms |
28160 KB |
Output is correct |
30 |
Incorrect |
24 ms |
28240 KB |
Output isn't correct |
31 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9676 KB |
Output is correct |
2 |
Correct |
5 ms |
9652 KB |
Output is correct |
3 |
Correct |
6 ms |
9732 KB |
Output is correct |
4 |
Correct |
5 ms |
9728 KB |
Output is correct |
5 |
Correct |
5 ms |
9676 KB |
Output is correct |
6 |
Correct |
5 ms |
9676 KB |
Output is correct |
7 |
Correct |
5 ms |
9676 KB |
Output is correct |
8 |
Correct |
5 ms |
9676 KB |
Output is correct |
9 |
Correct |
5 ms |
9676 KB |
Output is correct |
10 |
Correct |
5 ms |
9712 KB |
Output is correct |
11 |
Correct |
6 ms |
9676 KB |
Output is correct |
12 |
Correct |
5 ms |
9676 KB |
Output is correct |
13 |
Correct |
5 ms |
9688 KB |
Output is correct |
14 |
Correct |
5 ms |
9676 KB |
Output is correct |
15 |
Correct |
5 ms |
9612 KB |
Output is correct |
16 |
Correct |
6 ms |
9676 KB |
Output is correct |
17 |
Correct |
5 ms |
9676 KB |
Output is correct |
18 |
Correct |
6 ms |
9676 KB |
Output is correct |
19 |
Correct |
6 ms |
9932 KB |
Output is correct |
20 |
Correct |
8 ms |
11724 KB |
Output is correct |
21 |
Correct |
6 ms |
11628 KB |
Output is correct |
22 |
Correct |
174 ms |
91880 KB |
Output is correct |
23 |
Correct |
59 ms |
48808 KB |
Output is correct |
24 |
Correct |
40 ms |
48760 KB |
Output is correct |
25 |
Correct |
37 ms |
28260 KB |
Output is correct |
26 |
Correct |
59 ms |
28204 KB |
Output is correct |
27 |
Correct |
313 ms |
91968 KB |
Output is correct |
28 |
Correct |
20 ms |
28260 KB |
Output is correct |
29 |
Correct |
31 ms |
28160 KB |
Output is correct |
30 |
Incorrect |
24 ms |
28240 KB |
Output isn't correct |
31 |
Halted |
0 ms |
0 KB |
- |