#include <bits/stdc++.h>
#define int long long
using namespace std;
vector<int> path, fpath;
void DFS(int head, int par, vector<vector<pair<int, int>>> &adj){
if(head == adj.size() - 1){
fpath = path;
return;
}
for(auto[l, r] : adj[head]){
if(l != par){
path.push_back(r);
DFS(l, head, adj);
path.pop_back();
}
}
}
signed main(){
int N, M; cin >> N >> M;
vector<vector<pair<int, int>>> adj(N);
vector<int> vals(M);
for(int i = 0; i < M; i ++){
int u, v, w; cin >> u >> v >> w;
u--; v--;
adj[u].push_back({v, i});
adj[v].push_back({u, i});
vals[i] = w;
}
if(M == N - 1){
vector<int> maxsuff(N);
for(int i = N - 2; i >= 0; i--) maxsuff[i] = max(vals[i + 1], maxsuff[i + 1]);
DFS(0, -1, adj);
int ans = 0;
for(int i : fpath) ans = max(ans, maxsuff[i]);
for(int i : fpath) ans += vals[i];
cout << ans << '\n';
}
else{
int ans = 0;
for(int i = 0; i < M - 1; i++){
int tval = *max_element(vals.begin() + i + 1, vals.end());
vals[i] += tval;
priority_queue<vector<int>> q;
q.push({0, 0});
int curr = 0;
vector<bool> vis(N, false);
while(!q.empty()){
int a = -q.top()[0], b = q.top()[1];
q.pop();
if(b == N - 1){curr = a; break;}
if(vis[b])continue;
vis[b] = true;
for(auto [l, r] : adj[b]) q.push({-a - vals[r], l});
}
ans = max(ans, curr);
vals[i] -= tval;
}
cout << ans << '\n';
}
}
Compilation message
Aesthetic.cpp: In function 'void DFS(long long int, long long int, std::vector<std::vector<std::pair<long long int, long long int> > >&)':
Aesthetic.cpp:7:13: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::vector<std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
7 | if(head == adj.size() - 1){
| ~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
947 ms |
856 KB |
Output is correct |
10 |
Correct |
880 ms |
660 KB |
Output is correct |
11 |
Correct |
738 ms |
604 KB |
Output is correct |
12 |
Correct |
757 ms |
604 KB |
Output is correct |
13 |
Correct |
790 ms |
600 KB |
Output is correct |
14 |
Correct |
816 ms |
604 KB |
Output is correct |
15 |
Correct |
889 ms |
640 KB |
Output is correct |
16 |
Correct |
817 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
290 ms |
34800 KB |
Output is correct |
2 |
Correct |
273 ms |
35028 KB |
Output is correct |
3 |
Correct |
306 ms |
34420 KB |
Output is correct |
4 |
Correct |
274 ms |
34644 KB |
Output is correct |
5 |
Correct |
270 ms |
34640 KB |
Output is correct |
6 |
Correct |
291 ms |
35576 KB |
Output is correct |
7 |
Correct |
271 ms |
35228 KB |
Output is correct |
8 |
Correct |
296 ms |
35552 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2064 ms |
32852 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2073 ms |
39328 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2073 ms |
39328 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
2 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
947 ms |
856 KB |
Output is correct |
10 |
Correct |
880 ms |
660 KB |
Output is correct |
11 |
Correct |
738 ms |
604 KB |
Output is correct |
12 |
Correct |
757 ms |
604 KB |
Output is correct |
13 |
Correct |
790 ms |
600 KB |
Output is correct |
14 |
Correct |
816 ms |
604 KB |
Output is correct |
15 |
Correct |
889 ms |
640 KB |
Output is correct |
16 |
Correct |
817 ms |
604 KB |
Output is correct |
17 |
Correct |
290 ms |
34800 KB |
Output is correct |
18 |
Correct |
273 ms |
35028 KB |
Output is correct |
19 |
Correct |
306 ms |
34420 KB |
Output is correct |
20 |
Correct |
274 ms |
34644 KB |
Output is correct |
21 |
Correct |
270 ms |
34640 KB |
Output is correct |
22 |
Correct |
291 ms |
35576 KB |
Output is correct |
23 |
Correct |
271 ms |
35228 KB |
Output is correct |
24 |
Correct |
296 ms |
35552 KB |
Output is correct |
25 |
Execution timed out |
2064 ms |
32852 KB |
Time limit exceeded |
26 |
Halted |
0 ms |
0 KB |
- |