#include <bits/stdc++.h>
using namespace std;
const int N = 1000 + 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});
}
dfs(1);
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] + 1, 0, 0}) - e2.begin();
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";
}
Compilation message
training.cpp: In function 'int main()':
training.cpp:41: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]
41 | for(int i = 0; i < e.size(); ++i){
| ~~^~~~~~~~~~
training.cpp:49: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]
49 | for(int i = 0; i < e.size(); ++i){
| ~~^~~~~~~~~~
training.cpp:64: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]
64 | for(int i = 0; i < e2.size(); ++i)
| ~~^~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
460 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
328 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
332 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
460 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
360 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
504 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |