#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
const int N=1e3;
const int K=10;
const int L=10;
vector<int> te[N+10];
vector<tuple<int,int,int,int>> edg_wait;
vector<pair<int,int>> edg[N+10];
vector<pair<int,int>> up[N+10];
int dep[N+10];
int mp[N+10];
unordered_map<int,pair<int,int>> st;
unordered_set<int> st2;
void dfs_dep(int x,int p)
{
dep[x]=dep[p]+1;
for(auto v:te[x])
{
if(v!=p)
dfs_dep(v,x);
}
return;
}
int lsb(int x)
{
x=(x&(-x));
return __lg(x);
}
int dfs_ans(int x,int p)
{
int ans=0;
for(auto v:te[x])
{
if(v!=p)
ans+=dfs_ans(v,x);
}
int d[K][K];
int du[K];
int dp[(1<<K)];
st.clear();st2.clear();
for(int i=0;i<K;i++)
{
for(int j=0;j<K;j++)
d[i][j]=0;
du[i]=0;
}
int nr=0;
for(auto v:te[x])
{
if(v!=p)
mp[v]=nr++;
}
for(auto v:te[x])
{
if(v==p)
continue;
for(auto [id,c]:up[v])
{
if(st.find(id)!=st.end())
{
d[mp[v]][st[id].fi]=d[st[id].fi][mp[v]]=max(d[st[id].fi][mp[v]],c+st[id].se);
st2.insert(id);
}
st[id]={mp[v],c};
}
}
for(auto [id,c]:edg[x])
{
if(st.find(id)!=st.end())
{
du[st[id].fi]=max(du[st[id].fi],c+st[id].se);
st2.insert(id);
}
}
dp[0]=0;
for(int i=1;i<(1<<K);i++)
{
int v=lsb(i);
dp[i]=dp[i^(1<<v)]+du[v];
for(int j=0;j<K;j++)
{
if(i&(1<<j))
dp[i]=max(dp[i],dp[i^(1<<v)^(1<<j)]+d[v][j]);
}
}
for(auto v:te[x])
{
if(v==p)
continue;
for(auto vv:up[v])
{
if(st2.find(vv.fi)==st2.end())
up[x].emplace_back(vv.fi,vv.se+dp[(1<<K)-1-(1<<mp[v])]-dp[(1<<K)-1]);
}
}
for(auto v:edg[x])
{
if(st2.find(v.fi)==st2.end())
up[x].emplace_back(v);
}
//cerr<<x<<" "<<ans<<" "<<dp[(1<<K)-1]<<"\n";
return ans+dp[(1<<K)-1];
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n,m;
cin>>n>>m;
int ans=0;
for(int i=1;i<=m;i++)
{
int a,b,c;
cin>>a>>b>>c;
if(c==0)
{
te[a].push_back(b);
te[b].push_back(a);
}
else
edg_wait.emplace_back(a,b,c,i);
}
dfs_dep(1,0);
for(auto [a,b,c,i]:edg_wait)
{
if(dep[a]%2==dep[b]%2)
{
edg[a].emplace_back(i,c);
edg[b].emplace_back(i,c);
}
ans+=c;
}
cout<<ans-dfs_ans(1,0)/2<<"\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
332 KB |
Output is correct |
2 |
Correct |
2 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
588 KB |
Output is correct |
2 |
Correct |
3 ms |
640 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
64 ms |
6800 KB |
Output is correct |
2 |
Correct |
68 ms |
9252 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
2 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
332 KB |
Output is correct |
2 |
Correct |
2 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
468 KB |
Output is correct |
2 |
Correct |
5 ms |
460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
12 ms |
588 KB |
Output is correct |
2 |
Correct |
14 ms |
460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
720 KB |
Output is correct |
2 |
Correct |
22 ms |
848 KB |
Output is correct |
3 |
Correct |
24 ms |
1484 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
45 ms |
1528 KB |
Output is correct |
2 |
Correct |
50 ms |
1008 KB |
Output is correct |
3 |
Correct |
46 ms |
1144 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
21 ms |
716 KB |
Output is correct |
2 |
Correct |
28 ms |
2036 KB |
Output is correct |
3 |
Correct |
115 ms |
13648 KB |
Output is correct |
4 |
Correct |
34 ms |
3184 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
72 ms |
5368 KB |
Output is correct |
2 |
Correct |
123 ms |
15160 KB |
Output is correct |
3 |
Correct |
70 ms |
5372 KB |
Output is correct |
4 |
Correct |
42 ms |
1048 KB |
Output is correct |