#include <stdio.h>
#include <stdlib.h>
#define M 5005
#define N 1005
long long max(long long a, long long b) { return a > b ? a : b; }
void cmax(long long*a, long long b) { *a = max(*a, b); }
int n, m, m_, *eh[N], eo[N], dep[N];
int *fh[N], fo[N];
int id[N][N];
void pus_(int **eh, int *eo, int j) {
int o = eo[0]++;
if (0 == o) eh[0] = (int*)malloc(2 * sizeof **eh);
else if (0 == (o & o - 1)) eh[0] = (int*)realloc(eh[0], 2 * o * sizeof**eh);
eh[0][o] = j;
}
#define pus(eh, eo, j) pus_(&(eh), &(eo), j)
int rc[M], ru[M][2], par[N], temp;
void dfs(int u, int p) {
par[u] = p;
for (int v, j = 0; j < eo[u]; ++j) if ((v = eh[u][j]) != p) {
dep[v] = dep[u] + 1;
dfs(v, u);
}
}
int lca(int u, int v) {
while (dep[u] != dep[v])
if (dep[u] > dep[v]) u = par[u];
else v = par[v];
while (u != v) u = par[u], v = par[v];
return u;
}
long long dp[N][1024];
void efs(int u, int p) {
for (int i = 0; i < eo[u]; ++i) {
efs(eh[u][i], u);
dp[u][1 << i] = dp[eh[u][i]][(1 << eo[eh[u][i]]) - 1];
}
for (int j = 0; j < fo[u]; ++j) {
int e = fh[u][j];
long long cost = rc[e];
int mm = 0;
for (int k = 0; k < 2; ++k) {
int cur = ru[e][k], prev = -1;
while (cur != u) {
int mask = (1 << eo[cur]) - 1;
if (prev != -1)
mask &= ~(1 << id[cur][prev]);
cost += dp[cur][mask];
if (par[cur] == u)
mm |= 1 << id[u][cur];
prev=cur;
cur = par[cur];
}
}
cmax(&dp[u][mm], cost);
}
for (int i = 0; i < (1 << eo[u]); ++i) {
//forr (int j = 1; j < i; ++j)if((i|j)==i)
for (int j = i; j > 0; j = (j - 1) & i)
cmax(&dp[u][i], dp[u][j]+ dp[u][i ^ j]);
}
}
int main() {
scanf("%d%d", &n, &m);
for (int u, v, c, i = 0; i < m; ++i) {
scanf("%d%d%d", &u, &v, &c);
if (!c)
pus(eh[u], eo[u], v), pus(eh[v], eo[v], u);
else
ru[m_][0] = u, ru[m_][1] = v, rc[m_] = c, ++m_;
}
par[1] = 1;
dfs(1, 1);
for (int i = 2; i <= n; ++i) {
for (int j = 0; j < eo[i]; ++j) {
if (eh[i][j] == par[i])
eh[i][j] = eh[i][--eo[i]];
break;
}
}
for (int i = 1; i <= n; ++i)
for (int k = 0; k < eo[i]; ++k)
id[i][eh[i][k]] = k;
long long answer = 0;
for (int i = 0; i < m_; ++i) {
int a = lca(ru[i][0], ru[i][1]);
answer += rc[i];
if ((dep[ru[i][0]] + dep[ru[i][1]] - 2 * dep[a]) % 2) {
} else {
pus(fh[a], fo[a], i);
}
}
efs(1, 1);
printf("%lld\n", answer - dp[1][(1 << eo[1]) - 1]);
}
Compilation message
training.c: In function 'pus_':
training.c:18:26: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
18 | else if (0 == (o & o - 1)) eh[0] = (int*)realloc(eh[0], 2 * o * sizeof**eh);
| ~~^~~
training.c: In function 'main':
training.c:81:5: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
81 | scanf("%d%d", &n, &m);
| ^~~~~~~~~~~~~~~~~~~~~
training.c:83:9: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
83 | scanf("%d%d%d", &u, &v, &c);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
319 ms |
524288 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
330 ms |
524288 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
307 ms |
524288 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
303 ms |
524288 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
287 ms |
524288 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
333 ms |
524288 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
405 ms |
524288 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
321 ms |
524288 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
337 ms |
524288 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
309 ms |
524288 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
338 ms |
524288 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |