#include "dreaming.h"
#include <bits/stdc++.h>
using namespace std;
#define print(a) for(auto x : a) cout << x << ' '; cout << endl;
vector<vector<array<int, 2>>> graph;
int travelTime(int n, int m, int l, int a[], int b[], int t[]) {
graph.resize(n);
for(int i = 0; i < m; i ++)
graph[a[i]].push_back({b[i], t[i]}), graph[b[i]].push_back({a[i], t[i]});
vector<int> d;
vector<array<int, 2>> lnk(n, {-1, -1});
vector<bool> used(n);
for(int i = 0; i < n; i ++){
if(!used[i]){
set<array<int, 2>> st;
st.insert({0, i});
used[i] = 1;
int last = -1;
while(!st.empty()){
auto [x, y] = *st.begin();
st.erase(st.begin());
last = y;
for(auto z : graph[y]){
if(!used[z[0]]){
st.insert({x + z[1], z[0]});
used[z[0]] = 1;
}
}
}
used[last] = 0;
st.insert({0, last});
while(!st.empty()){
auto [x, y] = *st.begin();
st.erase(st.begin());
last = y;
for(auto z : graph[y]){
if(used[z[0]]){
lnk[z[0]] = {y, z[1]};
st.insert({x + z[1], z[0]});
used[z[0]] = 0;
}
}
}
used[i] = 1;
st.insert({0, i});
while(!st.empty()){
auto [x, y] = *st.begin();
st.erase(st.begin());
for(auto z : graph[y]){
if(!used[z[0]]){
st.insert({x + z[1], z[0]});
used[z[0]] = 1;
}
}
}
vector<int> p;
int sum = 0;
while(lnk[last][0] != -1){
p.push_back(lnk[last][1]);
sum += p.back();
last = lnk[last][0];
}
p.push_back(0);
int ans = 1e9, s = 0;
for(int i = 0; i < p.size(); i ++)
ans = min(ans, max(s, sum - s)), s += p[i];
d.push_back(ans);
}
}
if(d.size() == 1)
return d[0];
else if(d.size() == 2)
return d[0] + d[1] + l;
else{
sort(d.begin(), d.end());
int ans = 2e9;
int k = d.size();
for(int i = 0; i < k - 2; i ++)
ans = min(ans, max(d[k - 2] + d[k - 1] + 2 * l, d[i] + l + d[k - 1]));
ans = min(ans, max(d[k - 2] + d[k - 1] + l, d[k - 1] + d[k - 3] + 2 * l));
ans = min(ans, max(d[k - 2] + d[k - 1] + l, d[k - 2] + d[k - 3] + 2 * l));
return ans;
}
}
Compilation message
dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:72:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
72 | for(int i = 0; i < p.size(); i ++)
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
36 ms |
8148 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
36 ms |
8148 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
6148 KB |
Output is correct |
2 |
Correct |
23 ms |
6104 KB |
Output is correct |
3 |
Correct |
20 ms |
5980 KB |
Output is correct |
4 |
Correct |
22 ms |
6108 KB |
Output is correct |
5 |
Correct |
23 ms |
6044 KB |
Output is correct |
6 |
Correct |
25 ms |
6676 KB |
Output is correct |
7 |
Correct |
33 ms |
6280 KB |
Output is correct |
8 |
Correct |
20 ms |
5980 KB |
Output is correct |
9 |
Correct |
23 ms |
5976 KB |
Output is correct |
10 |
Correct |
21 ms |
6492 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
12 ms |
4056 KB |
Output is correct |
13 |
Correct |
11 ms |
4056 KB |
Output is correct |
14 |
Correct |
11 ms |
4132 KB |
Output is correct |
15 |
Correct |
11 ms |
4056 KB |
Output is correct |
16 |
Correct |
11 ms |
4056 KB |
Output is correct |
17 |
Correct |
11 ms |
4052 KB |
Output is correct |
18 |
Correct |
14 ms |
4056 KB |
Output is correct |
19 |
Correct |
12 ms |
4056 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
12 ms |
4232 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
36 ms |
8148 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |