#include <bits/stdc++.h>
using namespace std;
const int N = 2000 + 3;
const int M = 5000 + 3;
int n, m;
vector<int> adj[N];
vector<array<int, 3>> e, e2;
vector<int> arr, dp;
int pos[N];
void dfs(int u, int p = -1){
arr.push_back(u);
pos[u] = (int)arr.size() - 1;
for(int to: adj[u])
if(to != p)
dfs(to, u);
}
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
for(int i = 0; i < m; ++i){
int 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});
}
for(int i = 1; i <= n; ++i)
if(adj[i].size() == 1){
dfs(i);
break;
}
for(int i = 0; i < e.size(); ++i){
auto [a, b, c] = e[i];
int new_a = pos[a];
int new_b = pos[b];
e[i] = {min(new_a, new_b), max(new_a, new_b), c};
}
int ans = 0;
for(int 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(int i = (int)e2.size() - 1; i >= 0; --i){
int next_idx = lower_bound(e2.begin(), e2.end(), array{e2[i][1], 0, 0}) - 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(int 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:45:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | for(int i = 0; i < e.size(); ++i){
| ~~^~~~~~~~~~
training.cpp:53:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
53 | for(int i = 0; i < e.size(); ++i){
| ~~^~~~~~~~~~
training.cpp:70:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
70 | for(int i = 0; i < e2.size(); ++i)
| ~~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
368 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
460 KB |
Output is correct |
2 |
Correct |
2 ms |
460 KB |
Output is correct |
# |
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 |
1 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
3 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 |
2 ms |
460 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |