//#pragma GCC optimize("Ofast,unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef long long llo;
#define mp make_pair
#define pb push_back
#define a first
#define b second
#define endl '\n'
llo n,m;
vector<llo> adj[2001];
llo pos=0;
llo ind[2001];
vector<llo> ss;
vector<pair<llo,llo>> pre[2001];
llo dp[2001];
void dfs(llo no){
pos++;
ss.pb(no);
ind[no]=pos;
for(auto j:adj[no]){
if(ind[j]==0){
dfs(j);
}
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>n>>m;
vector<pair<pair<llo,llo>,llo>> ed;
for(llo i=0;i<m;i++){
llo aa,bb,cc;
cin>>aa>>bb>>cc;
aa--;
bb--;
if(cc==0){
adj[aa].pb(bb);
adj[bb].pb(aa);
}
else{
ed.pb({{aa,bb},cc});
}
}
llo cur=0;
for(llo i=0;i<n;i++){
if(adj[i].size()==1){
cur=i;
}
}
dfs(cur);
llo ans=0;
for(auto j:ed){
if((abs(ind[j.a.a]-ind[j.a.b])%2==1)){
ans+=j.b;
}
else{
if(ind[j.a.a]<ind[j.a.b]){
pre[j.a.b].pb({j.a.a,j.b});
}
else{
pre[j.a.a].pb({j.a.b,j.b});
}
}
}
for(llo i=0;i<n;i++){
for(auto j:pre[ss[i]]){
dp[i+1]=max(dp[i+1],dp[ind[j.a]]+j.b);
ans+=j.b;
}
dp[i+1]=max(dp[i+1],dp[i]);
}
ans-=dp[n];
cout<<ans<<endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
492 KB |
Output is correct |
2 |
Correct |
1 ms |
492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
492 KB |
Output is correct |
2 |
Correct |
1 ms |
492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
620 KB |
Output is correct |
2 |
Correct |
2 ms |
748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
492 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
492 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
492 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
492 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
620 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
748 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
768 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
748 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |