#include <bits/stdc++.h>
using namespace std;
const long long N = 2000 + 3;
const long long M = 5000 + 3;
long long n, m;
vector<long long> adj[N];
vector<array<long long, 3>> e, e2;
vector<long long> arr, dp;
long long pos[N];
void dfs(long long u, long long p = -1){
arr.push_back(u);
pos[u] = (long long)arr.size() - 1;
for(long long to: adj[u])
if(to != p)
dfs(to, u);
}
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
for(long long i = 0; i < m; ++i){
long long a, b, c;
cin >> a >> b >> c;
if(!c){
adj[a].push_back(b);
adj[b].push_back(a);
}
else e.push_back({a, b, c});
}
dfs(1);
for(long long i = 0; i < e.size(); ++i){
auto [a, b, c] = e[i];
long long new_a = pos[a];
long long new_b = pos[b];
e[i] = {min(new_a, new_b), max(new_a, new_b), c};
}
long long ans = 0;
for(long long i = 0; i < e.size(); ++i){
auto [a, b, c] = e[i];
if((b - a) & 1) ans += c;
else e2.push_back({a, b, c});
}
sort(e2.begin(), e2.end());
dp.resize(e2.size() + 1);
dp[e2.size()] = 0;
for(long long i = (long long)e2.size() - 1; i >= 0; --i){
long long next_idx = lower_bound(e2.begin(), e2.end(), array{e2[i][1] + 1, 0ll, 0ll}) - e2.begin();
next_idx = max(next_idx, i + 1);
dp[i] = max(dp[i + 1], e2[i][2] + dp[next_idx]);
}
ans -= dp[0];
for(long long i = 0; i < e2.size(); ++i)
ans += e2[i][2];
cout << ans << "\n";
}
/*
3 3
1 3 1
1 2 0
2 3 0
*/
Compilation message
training.cpp: In function 'int main()':
training.cpp:41:28: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::array<long long int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
41 | for(long long i = 0; i < e.size(); ++i){
| ~~^~~~~~~~~~
training.cpp:49:28: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::array<long long int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
49 | for(long long i = 0; i < e.size(); ++i){
| ~~^~~~~~~~~~
training.cpp:66:28: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::array<long long int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
66 | for(long long i = 0; i < e2.size(); ++i)
| ~~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
460 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
460 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 ms |
588 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
460 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
588 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |