#include <bits/stdc++.h>
using namespace std;
#define ff first
#define ss second
int N, M;
struct ed{int a,b,c;};
vector<int> edge[1001], D[1001];
vector<ed> A[1001], E;
int dp[1001][1024], nth[1001], prt[10][1001], deg[1001], dth[1001];
void dfs(int x){
D[dth[x]].push_back(x);
for(auto &i: edge[x]){
if(prt[0][x]==i) continue;
prt[0][i] = x;
dth[i] = dth[x]+1;
dfs(i);
nth[i] = deg[x];
++deg[x];
}
}
int main(){
ios_base::sync_with_stdio(0), cin.tie(0);
cin>>N>>M;
int ans = 0;
for(int i=0; i<M; ++i){
int x,y,z; cin>>x>>y>>z;
if(z) E.push_back({z, x, y}), ans += z;
else edge[x].push_back(y), edge[y].push_back(x);
}
dth[1] = 1;
dfs(1);
for(int i=1; i<10; ++i) for(int j=1; j<=N; ++j) prt[i][j] = prt[i-1][prt[i-1][j]];
for(auto &i: E){
if((dth[i.b]+dth[i.c]) & 1) continue;
if(dth[i.b]<dth[i.c]) swap(i.b, i.c);
int x, y; tie(x, y) = tie(i.b, i.c);
for(int i=9; i>=0; --i) if(dth[prt[i][x]] >= dth[y]) x = prt[i][x];
if(x==y){
A[x].push_back(i);
continue;
}
for(int i=9; i>=0; --i) if(prt[i][x] != prt[i][y]) x = prt[i][x], y = prt[i][y];
A[prt[0][x]].push_back(i);
}
for(int d=1000; d; --d){
for(auto &x: D[d]){
for(int msk=1; msk<(1<<deg[x]); ++msk){
for(auto &i: edge[x]) if(prt[0][i]==x && ((msk>>nth[i]) & 1)) dp[x][msk] = max(dp[x][msk], dp[i][(1<<deg[i])-1]+dp[x][msk ^ (1<<nth[i])]);
for(auto &i: A[x]){
int p = i.b, q = i.c;
for(int i=9; i>=0; --i) if(dth[prt[i][p]]>dth[x]) p = prt[i][p];
for(int i=9; i>=0; --i) if(dth[prt[i][q]]>dth[x]) q = prt[i][q];
if(!(1 & (msk>>nth[p]) & (q!=x?(msk>>nth[q]):1))) continue;
int m = msk ^ ((1<<nth[p]) | (q!=x?(1<<nth[q]):0));
if(!m){
int t = i.a + dp[i.b][(1<<deg[i.b])-1];
for(int v=i.b; prt[0][v]!=x; v=prt[0][v]) t += dp[prt[0][v]][((1<<deg[prt[0][v]])-1) ^ (1<<nth[v])];
if(i.c != x){
t += dp[i.c][(1<<deg[i.c])-1];
for(int v=i.c; prt[0][v]!=x; v=prt[0][v]) t += dp[prt[0][v]][((1<<deg[prt[0][v]])-1) ^ (1<<nth[v])];
}
dp[x][msk] = max(dp[x][msk], t);
}
else dp[x][msk] = max(dp[x][msk], dp[x][m]+dp[x][msk ^ m]);
}
}
}
}
cout<<ans-dp[1][(1<<deg[1])-1];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
512 KB |
Output is correct |
2 |
Correct |
3 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
640 KB |
Output is correct |
2 |
Correct |
3 ms |
640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
4512 KB |
Output is correct |
2 |
Correct |
11 ms |
4608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
512 KB |
Output is correct |
2 |
Correct |
2 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
512 KB |
Output is correct |
2 |
Correct |
0 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
684 KB |
Output is correct |
2 |
Correct |
3 ms |
700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
896 KB |
Output is correct |
2 |
Correct |
1 ms |
768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
1124 KB |
Output is correct |
2 |
Correct |
9 ms |
896 KB |
Output is correct |
3 |
Correct |
71 ms |
1656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
2260 KB |
Output is correct |
2 |
Correct |
87 ms |
1220 KB |
Output is correct |
3 |
Correct |
18 ms |
1536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
69 ms |
1016 KB |
Output is correct |
2 |
Correct |
7 ms |
1764 KB |
Output is correct |
3 |
Correct |
151 ms |
4564 KB |
Output is correct |
4 |
Correct |
9 ms |
1792 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
2224 KB |
Output is correct |
2 |
Correct |
84 ms |
4484 KB |
Output is correct |
3 |
Correct |
45 ms |
1780 KB |
Output is correct |
4 |
Correct |
76 ms |
1528 KB |
Output is correct |