#include <utility>
#include <iostream>
#include <vector>
#include <string>
#define N 1005
#define BIT(x,i) ((x>>i)&1)
using namespace std;
vector <int> a[N];
class edge {
public:
int u, v, k;
};
vector <edge> c[N], add;
long long n, m, i, u, v, sum, f[N], g[N], p[N], h[N], k, num[N][N], w[11], w2[11][11], dp[N][11][1024], nchild[N];
void dfs(int u)
{
for (int i = 0; i < a[u].size(); i++)
{
int v = a[u][i];
if (v != p[u]) h[v] = h[u] + 1, p[v] = u, dfs(v);
}
}
void opt(long long& a, long long b) {
a = max(a, b);
}
int LCA(int u, int v) {
while (h[u] > h[v]) {
u = p[u];
}
while (h[v] > h[u]) {
v = p[v];
}
while (u != v) {
u = p[u], v = p[v];
}
return u;
}
int nearest(int u, int v) {
while (p[u] != v) {
u = p[u];
}
return u;
}
long long climb(int u, int v) {
long long res = 0, last;
res += f[u]; last = u; u = p[u];
while (u != v) {
res += dp[u][nchild[u] - 1][((1 << nchild[u]) - 1) ^ (1 << num[u][last])];
last = u; u = p[u];
}
return res;
}
void DFS(int u) {
int i, j, tt;
for (int i = 0; i < a[u].size(); i++) {
int v = a[u][i];
if (v != p[u]) {
DFS(v);
}
}
for (int v : a[u]) {
if (v != p[u]) {
g[nchild[u]] = f[v], num[u][v] = nchild[u]++;
}
}
memset(w, 0, sizeof(w));
memset(w2, 0, sizeof(w2));
for (int i = 0; i < c[u].size(); i++) {
edge x = c[u][i];
int v1 = x.u, v2 = x.v, k = x.k, tg1, tg2;
long long sum, sum1, sum2;
if (v2 == u) {
tg1 = num[u][nearest(v1, u)];
opt(w[tg1], climb(v1, u) + k);
} else {
tg1 = num[u][nearest(v1, u)]; sum1 = climb(v1, u);
tg2 = num[u][nearest(v2, u)]; sum2 = climb(v2, u);
opt(w2[tg1][tg2], sum1 + sum2 + k);
opt(w2[tg2][tg1], sum1 + sum2 + k);
}
}
if (nchild[u] == 0) return;
dp[u][0][0] = 0;
dp[u][0][1] = max(w[0], g[0]);
for (i = 1; i < nchild[u]; i++) {
for (tt = 0; tt < (1 << i); tt++) {
opt(dp[u][i][tt], dp[u][i - 1][tt]);
opt(dp[u][i][tt | (1 << i)], dp[u][i - 1][tt] + max(g[i], w[i]));
for (j = 0; j < i; j++)
if (BIT(tt, j) == 0) {
opt(dp[u][i][(tt | (1 << i)) | (1 << j)], dp[u][i - 1][tt] + max(w2[i][j], g[i] + g[j]));
}
}
}
f[u] = dp[u][nchild[u] - 1][(1 << nchild[u]) - 1];
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> m;
for (int i = m; i >= 1; i--) {
cin >> u >> v >> k;
edge tg = { u,v,k };
if (k == 0) a[u].push_back(v), a[v].push_back(u);
else add.push_back(tg), sum += k;
}
dfs(1);
for (i = 0; i < add.size(); i++) {
edge x = add[i];
int u = x.u, v = x.v, k = x.k, p = LCA(u, v);
if (h[u] < h[v]) swap(u, v);
if ((h[u] + h[v] - 2 * h[p]) & 1) continue;
edge tg = {
u,v,k
};
c[p].push_back(tg);
}
DFS(1);
cout << sum - f[1];
}
Compilation message
training.cpp: In function 'void dfs(int)':
training.cpp:24:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
24 | for (int i = 0; i < a[u].size(); i++)
| ~~^~~~~~~~~~~~~
training.cpp: In function 'void DFS(int)':
training.cpp:74:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
74 | for (int i = 0; i < a[u].size(); i++) {
| ~~^~~~~~~~~~~~~
training.cpp:88:5: error: 'memset' was not declared in this scope
88 | memset(w, 0, sizeof(w));
| ^~~~~~
training.cpp:4:1: note: 'memset' is defined in header '<cstring>'; did you forget to '#include <cstring>'?
3 | #include <vector>
+++ |+#include <cstring>
4 | #include <string>
training.cpp:91:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<edge>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
91 | for (int i = 0; i < c[u].size(); i++) {
| ~~^~~~~~~~~~~~~
training.cpp:94:19: warning: unused variable 'sum' [-Wunused-variable]
94 | long long sum, sum1, sum2;
| ^~~
training.cpp: In function 'int main()':
training.cpp:128:21: warning: narrowing conversion of 'u' from 'long long int' to 'int' [-Wnarrowing]
128 | edge tg = { u,v,k };
| ^
training.cpp:128:23: warning: narrowing conversion of 'v' from 'long long int' to 'int' [-Wnarrowing]
128 | edge tg = { u,v,k };
| ^
training.cpp:128:25: warning: narrowing conversion of 'k' from 'long long int' to 'int' [-Wnarrowing]
128 | edge tg = { u,v,k };
| ^
training.cpp:134:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<edge>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
134 | for (i = 0; i < add.size(); i++) {
| ~~^~~~~~~~~~~~