| Task: | Shortest Routes I |
| Sender: | aalto26fh_034 |
| Submission time: | 2026-09-30 19:56:49 +0300 |
| Language: | C++ (C++20) |
| Status: | READY |
| Result: | TIME LIMIT EXCEEDED |
| test | verdict | time | |
|---|---|---|---|
| #1 | ACCEPTED | 0.00 s | details |
| #2 | ACCEPTED | 0.00 s | details |
| #3 | ACCEPTED | 0.00 s | details |
| #4 | ACCEPTED | 0.00 s | details |
| #5 | ACCEPTED | 0.00 s | details |
| #6 | ACCEPTED | 1.93 s | details |
| #7 | TIME LIMIT EXCEEDED | -- | details |
| #8 | TIME LIMIT EXCEEDED | -- | details |
| #9 | TIME LIMIT EXCEEDED | -- | details |
| #10 | TIME LIMIT EXCEEDED | -- | details |
| #11 | ACCEPTED | 0.16 s | details |
| #12 | ACCEPTED | 0.14 s | details |
| #13 | ACCEPTED | 0.00 s | details |
| #14 | ACCEPTED | 0.15 s | details |
| #15 | ACCEPTED | 0.15 s | details |
| #16 | ACCEPTED | 0.13 s | details |
| #17 | ACCEPTED | 0.13 s | details |
| #18 | ACCEPTED | 0.17 s | details |
Compiler report
input/code.cpp: In function 'char getNthCharOption(std::string, Int, char)':
input/code.cpp:551:12: warning: comparison of integer expressions of different signedness: 'Int' {aka 'long long int'} and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
551 | if ( i < a.size()){
| ~~^~~~~~~~~~Code
#include <iostream>
#include <vector>
#include <functional>
#include <set>
#include <algorithm>
#include <stack>
#include <unordered_map>
#include <map>
#include <iomanip>
#include <limits>
#include <queue>
typedef long long Int;
std::tuple<Int> readInt1(){
Int a;
std::cin >> a;
return {a};
}
std::tuple<Int,Int> readInt2(){
Int a, b;
std::cin >> a >> b;
return {a,b};
}
std::tuple<Int,Int,Int> readInt3(){
Int a, b, c;
std::cin >> a >> b >> c;
return {a,b,c};
}
std::vector<Int> readVecInt(Int n){
std::vector<Int> arr(n);
for (Int i = 0; i < n; i++){
Int a;
std::cin >> a;
arr[i] = a;
}
return arr;
}
std::vector<std::string> readVecStr(Int n){
std::vector<std::string> arr(n);
for (Int i = 0; i < n; i++){
std::string a;
std::cin >> a;
arr[i] = a;
}
return arr;
}
std::vector<long long> readVecLong(Int n){
std::vector<long long> arr(n);
for (Int i = 0; i < n; i++){
long long a;
std::cin >> a;
arr[i] = a;
}
return arr;
}
std::vector<std::tuple<Int,Int>> readVecTup2(Int n){
std::vector<std::tuple<Int,Int>> arr(n);
for (Int i = 0; i < n; i++){
Int a, b;
std::cin >> a;
std::cin >> b;
arr[i] = {a,b};
}
return arr;
}
std::vector<std::tuple<Int,Int,Int>> readVecTup3(Int n){
std::vector<std::tuple<Int,Int,Int>> arr(n);
for (Int i = 0; i < n; i++){
Int a, b, c;
std::cin >> a;
std::cin >> b;
std::cin >> c;
arr[i] = {a,b,c};
}
return arr;
}
std::vector<std::tuple<Int,Int,Int,Int>> readVecTup4(Int n){
std::vector<std::tuple<Int,Int,Int,Int>> arr(n);
for (Int i = 0; i < n; i++){
Int a, b, c,d;
std::cin >> a;
std::cin >> b;
std::cin >> c;
std::cin >> d;
arr[i] = {a,b,c,d};
}
return arr;
}
void printNewLine(){std::cout << "\n";}
template<typename T, typename... Ts>
std::tuple<Ts...> tail(std::tuple<T,Ts...> as){
return std::apply([](T &a, Ts... b){return std::make_tuple(b...);},as);
}
template<typename T>
void print(T a){std::cout << std::setprecision(19) << a;}
template<typename... T>
void println(T... a);
template<typename T, typename Ts>
void print(std::tuple<T,Ts> a){
print(std::get<0>(a));
print(" ");
print(std::get<1>(a));
}
template<typename T, typename... Ts>
void print(std::tuple<T,Ts...> a){
print(std::get<0>(a));
print(" ");
print(tail(a));
}
template<typename T>
void print(const std::vector<T> &arr, Int width = -1){
if (width == -1) width = arr.size();
for (Int i = 0; i < (Int)arr.size(); i++){
if ((i%width) == width-1 && i != (Int)arr.size()-1){
print(" ");
println(arr[i]);
}else if ((i%width) == 0){
print(arr[i]);
}else {
print(" ");
print(arr[i]);
}
}
}
template<typename... T>
void println(T... a){print(a...);printNewLine();}
template<typename T>
void sorted(std::vector<T> &arr){
std::sort(arr.begin(),arr.end(),[](const T &a, const T &b){ return a < b; });
}
template<typename T>
std::set<T> toSet(const std::vector<T> &vec){
std::set<T> c;
for (T a : vec){ c.emplace(a);}
return c;
}
template<typename T, typename N>
std::vector<N> map(const std::vector<T> &a, const std::function<N(T)> &transform){
std::vector<N> b(a.size());
Int i = 0;
for (T x : a){
b[i] = transform(x);
i++;
}
return b;
}
template<typename N, typename M, typename R>
std::vector<R> zip(const std::vector<N> &a, const std::vector<M> &b, const std::function<R(N,M)> &transform){
Int n = std::min(a.size(),b.size());
std::vector<R> res(n);
for (Int i = 0; i < n; i++){
res[i] = transform(a[i],b[i]);
}
return res;
}
template<typename T, typename N>
std::vector<N> convertVec(const std::vector<T> &a){
std::vector<N> b(a.size());
Int i = 0;
for (T x : a){
b[i] = x;
i++;
}
return b;
}
template<typename T>
std::vector<T> reverse(const std::vector<T> &a){
std::vector<T> b(a.size());
Int i = 0;
for (T x : a){
b[a.size()-1-i] = x;
i++;
}
return b;
}
template<typename T, typename R>
R reduce(const std::vector<T> &arr, R init, const std::function<R(const R,const T)> &transform){
R current = init;
for (Int idx = 0; idx < (Int)arr.size(); idx++){
current = transform(current,arr[idx]);
};
return current;
}
template<typename T>
T max(const std::vector<T> &arr){return reduce(arr, arr[0], static_cast<std::function<T(const T,const T)>>(static_cast<const T& (*)(const T&, const T&)>(std::max<T>)));}
template<typename T>
T min(const std::vector<T> &arr){return reduce(arr, arr[0], static_cast<std::function<T(const T,const T)>>(static_cast<const T& (*)(const T&, const T&)>(std::min<T>)));}
template<typename T>
std::function<T(const T,const T)> add(){return [](T a,T b){return a+b;};}
template<typename T>
std::function<T(const T,const T)> modAdd(T mod){return [mod](T a,T b){return (((a+b)%mod)+mod)%mod;};}
template<typename T, typename R>
std::function<R(const R,const T)> mulAdd(R mul){return [mul](R a,T b){return a+b*mul;};}
template<typename T>
T sum(const std::vector<T> &arr){return reduce(arr, 0, add<T>());}
template<typename T>
T avg(const std::vector<T> &arr){return reduce(arr, 0.0, mulAdd<double,T>(1.0/arr.size()));}
template<typename T>
T binarySearch(T min, T max, const std::function<bool(T)> &leq){
if (min == max) return max;
T guess = (min+max)/2;
if (leq(guess)){
return binarySearch(min,guess,leq);
}else{
return binarySearch(guess+1,max,leq);
}
}
template<typename T>
T binarySearchDecending(T min, T max, const std::function<bool(T)> &leq){
std::function<bool(T)> gt = [leq](T x){
return !leq(x);
};
return binarySearch(min,max+1, gt)-1;
}
template<typename T>
std::vector<T> cloneVec(std::vector<T> a){return a;}
template<typename T>
std::vector<T> padding(const std::vector<T> &initial, T num, Int start, Int end){
std::vector<T> arr(start+initial.size()+end);
for (Int i = 0; i < start; i++){
arr[i] = num;
}
for (Int i = 0; i < (Int)initial.size(); i++){
arr[start+i] = initial[i];
}
for (Int i = 0; i < end; i++){
arr[start+initial.size()+i] = num;
}
return arr;
}
template<typename T>
Int findMax(Int start, Int end, const std::function<T(Int)> &maxHeuristic){
T initialGuess = maxHeuristic(start);
Int index = start;
for (Int i = start; i <= end; i++){
if (initialGuess < maxHeuristic(i)){
initialGuess = maxHeuristic(i);
index = i;
}
}
return index;
}
template<typename T>
void operateOnMax(Int start, Int end, const std::function<T(Int)> &maxHeuristic, const std::function<void(Int)> &found){
found(findMax(start,end,maxHeuristic));
}
template<typename T>
void stackOperate(T init, const std::function<std::vector<T>(T)> operate){
std::stack<T> stack;
stack.push(init);
while (!stack.empty()){
T next = stack.top();
stack.pop();
std::vector<T> nextSteps = operate(next);
for (T a : nextSteps){
stack.push(a);
}
}
}
template<typename T>
void stackOperate(std::vector<T> init, const std::function<std::vector<T>(T)> operate){
std::stack<T> stack;
for (auto x : init){
stack.push(x);
}
while (!stack.empty()){
T next = stack.top();
stack.pop();
std::vector<T> nextSteps = operate(next);
for (T a : nextSteps){
stack.push(a);
}
}
}
template<typename T>
void queueOperate(T init, const std::function<std::vector<T>(T)> operate){
std::queue<T> que;
que.push(init);
while (!que.empty()){
T next = que.front();
que.pop();
std::vector<T> nextSteps = operate(next);
for (T a : nextSteps){
que.push(a);
}
}
}
template<typename T>
void queueOperate(std::vector<T> init, const std::function<std::vector<T>(T)> operate){
std::queue<T> que;
for (auto x : init){
que.push(x);
}
while (!que.empty()){
T next = que.front();
que.pop();
std::vector<T> nextSteps = operate(next);
for (T a : nextSteps){
que.push(a);
}
}
}
template<typename T>
void recursiveVectorSplit(Int start, Int end, Int distl, Int distr, const std::function<T(Int)> &maxHeuristic, const std::function<void(Int)> &found){
if (start == end){
return found(start);
}else if (end < start){
return;
}
std::tuple<Int,Int> range = {start,end};
std::function<std::vector<std::tuple<Int,Int>>(std::tuple<Int,Int>)> splitter = [distl, distr,&maxHeuristic,&found](std::tuple<Int,Int> range){
auto [start,end] = range;
Int idx = findMax(start, end, maxHeuristic);
found(idx);
std::vector<std::tuple<Int,Int>> next;
if (idx+distl+1 <= end){
next.push_back({idx+distl+1,end});
}
if (start <= idx-distr-1){
next.push_back({start, idx-distr-1});
}
return next;
};
stackOperate(range,splitter);
}
template<typename T>
std::vector<T> dynamicallyComputableList(Int n, std::function<T(std::vector<T>&,Int)> next){
std::vector<T> arr(n);
for (Int i = 0; i < n; i++){
arr[i] = next(arr,i);
}
return arr;
}
template<typename T>
Int indexOfBinarySearch(const std::vector<T> &arr, const T &find){
return binarySearch((Int)0,(Int)arr.size()-1,(std::function<bool(Int)>)[&arr, &find](Int guess){return find <= arr[guess];});
}
template<typename T>
std::unordered_map<T, Int> indexCompress(const std::vector<T> &arr){
std::unordered_map<T, Int> compressed;
Int i = 0;
for (T x : arr){
if (compressed.find(x) != compressed.end()) compressed[x] = i++;
}
return compressed;
}
template<typename T>
std::tuple<std::vector<T>, std::vector<Int>> shortestPath(const std::vector<std::tuple<Int, Int, T>> &edgesMessy, Int start){
std::vector<std::tuple<Int, Int, T>> edges = edgesMessy;
for (auto e : edgesMessy){
auto [a,b,t] = e;
edges.push_back({b,a,t});
}
shortestPathDirected(edges, start);
}
template<typename T>
std::tuple<std::vector<T>, std::vector<Int>> shortestPathDirected(const std::vector<std::tuple<Int, Int, T>> &edges, Int start){
//assumes all are between 0 <= n < c*(number of vertexies), where c is reasonably small constant.
Int n = 0;
for (auto& [s,g,c] : edges){
n = std::max(n,s);
n = std::max(n,g);
}
Int mappedStart = start;
n += 1;
std::vector<T> distance(n);
std::vector<Int> path(n);
for (Int i = 0; i < n; i++) distance[i] = std::numeric_limits<T>::max()/4;
for (Int i = 0; i < n; i++) path[i] = i;
distance[mappedStart] = 0;
for (Int i = 1; i <= n; i++) {
bool changed = false;
for (auto e : edges) {
auto [a, b, w] = e;
if (!changed) {changed = (distance[b] != std::min(distance[b], distance[a]+w));}
if (distance[a]+w < distance[b]){
distance[b] = distance[a]+w;
path[b] = a;
}
}
if (!changed) break;
if (changed && i == n){
return std::tuple((std::vector<T>){},(std::vector<Int>){});
}
}
return std::tuple(distance,path);
}
std::vector<Int> pathFromPathMap(std::vector<Int> map, Int goal){
std::stack<Int> fullPath;
fullPath.push(goal);
while (map[fullPath.top()] != fullPath.top()){
fullPath.push(map[fullPath.top()]);
}
std::vector<Int> p;
while (!fullPath.empty()){
p.push_back(fullPath.top());
fullPath.pop();
}
return p;
}
template<typename T>
std::vector<T> distanceTable(const std::vector<T> &init, const std::function<T(T,T)> &distance){
Int n = init.size();
std::vector<T> table(n*n);
for (Int i = 0; i < n; i++){
table[i+n*i] = init[i];
for (Int j = i-1; j >= 0; j--){
table[j+n*i] = distance(init[j],table[j+1+n*i]);
}
for (Int j = i+1; j < n; j++){
table[j+n*i] = distance(table[j-1+n*i],init[j]);
}
}
return table;
}
void particles(){
auto [n] = readInt1();
auto arr = readVecLong(n);
std::vector<long long> table = distanceTable(arr,add<long long>());
print(table,n);
//println(cost[n*n-1]);
}
template<typename T>
std::vector<T> range(const std::vector<T> &a, Int from, Int to){
std::vector<T> b;
for (Int i = from; i < to; i++){
b.push_back(a[i]);
}
return b;
}
template<typename T>
bool forall(const std::vector<T> &a, std::function<bool(T)> &test){
bool res = true;
for (auto v : a){
res = res & test(v);
}
return res;
}
/*
std::vector<Int> dynamicUpdater(std::vector<std::tuple<Int,Int>> get, std::function<Int(std::unordered_map<std::tuple<Int,Int>, Int>&,std::tuple<Int,Int>)> eval, std::function<std::vector<std::tuple<Int,Int>>(std::tuple<Int,Int>)> needs){
std::map<std::tuple<Int,Int>, Int> results;
std::stack<std::tuple<Int,Int>> toDo;
for (auto val : get){
toDo.push(val);
}
std::function<bool(std::tuple<Int,Int>)> has = [&results](std::tuple<Int,Int> &value){
return results.find(value) != results.end();
};
while (toDo.size() != 0){
auto next = toDo.top();
toDo.pop();
if (results.find(next) != results.end()){
} else if (forall(needs(next))){
results[next] = eval(results,next);
} else {
for (auto val : needs(next)){
toDo.push(val);
}
}
}
std::function<Int(std::tuple<Int,Int>)> resultMap = [&results](std::tuple<Int,Int> &value){
return results[value];
};
return map(get,resultMap);
}*/
/*
template<typename T, typename R>
std::vector<R> dynamicUpdater(std::vector<T> get, std::function<R(std::unordered_map<T, R>&,T)> eval, std::function<std::vector<T>(T)> needs){
std::map<T, R> results;
std::stack<T> toDo;
for (auto val : get){
toDo.push(val);
}
std::function<bool(T)> has = [&results](T &value){
return results.find(value) != results.end();
};
while (toDo.size() != 0){
auto next = toDo.top();
toDo.pop();
if (results.find(next) != results.end()){
} else if (forall(needs(next))){
results[next] = eval(results,next);
} else {
for (auto val : needs(next)){
toDo.push(val);
}
}
}
std::function<R(T)> resultMap = [&results](T &value){
return results[value];
};
return map(get,resultMap);
}
*/
char getNthCharOption(std::string a, Int i,char opt){
if ( i < a.size()){
return a[i];
} else {
return opt;
}
}
/*
typedef std::tuple<char,char,char,char,char,char,char,char,char,char,std::string> tup11;
void lexicographicallyMinimal(std::vector<std::string> &arr,){
if (arr.size() == 0) return;
sorted(arr);
std::string min = arr[0];
Int size = min.size();
char minc = arr[0][0];
Int count = 0;
while (count < arr.size()){
if (arr[count].rfind(minc) != 0) break;
std::string cand = "";
for (Int n = size; n < arr[count].size();n++){
cand += arr[n];
}
cand
count += 1;
}
if (count == 0) print(arr[0]);
return lexicographicallyMinimal()
}
void lexicographicallyMinimal(std::vector<std::string> &arr){
if (arr.size() == 0) return;
sorted(arr);
std::string min = arr[0];
Int size = min.size();
char minc = arr[0][0];
Int count = 0;
while (count < arr.size()){
if (arr[count].rfind(minc) != 0) break;
std::string cand = "";
for (Int n = size; n < arr[count].size();n++){
cand += arr[n];
}
cand
count += 1;
}
if (count == 0) print(arr[0]);
return lexicographicallyMinimal()
}*/
void shortestRoutes(){
auto [n,m] = readInt2();
auto arr = readVecTup3(m);
auto [paths,route] = shortestPathDirected(arr,1);
println(range(paths,1,paths.size()));
}
int main() {
shortestRoutes();
return 0;
}Test details
Test 1
Verdict: ACCEPTED
| input |
|---|
| 10 20 8 5 1 9 10 2 7 9 8 9 8 8 ... |
| correct output |
|---|
| 0 9 11 20 13 14 19 29 27 29 |
| user output |
|---|
| 0 9 11 20 13 14 19 29 27 29 |
Test 2
Verdict: ACCEPTED
| input |
|---|
| 10 20 5 6 4 5 1 7 7 4 4 7 8 1 ... |
| correct output |
|---|
| 0 7 9 17 15 17 21 22 25 30 |
| user output |
|---|
| 0 7 9 17 15 17 21 22 25 30 |
Test 3
Verdict: ACCEPTED
| input |
|---|
| 10 20 1 4 1 4 2 1 9 10 1 1 2 4 ... |
| correct output |
|---|
| 0 2 11 1 2 7 16 18 12 13 |
| user output |
|---|
| 0 2 11 1 2 7 16 18 12 13 |
Test 4
Verdict: ACCEPTED
| input |
|---|
| 10 20 6 3 5 7 5 8 5 1 8 8 9 5 ... |
| correct output |
|---|
| 0 5 9 18 22 10 14 23 27 36 |
| user output |
|---|
| 0 5 9 18 22 10 14 23 27 36 |
Test 5
Verdict: ACCEPTED
| input |
|---|
| 10 20 8 9 3 2 3 8 10 5 3 2 5 3 ... |
| correct output |
|---|
| 0 8 16 18 11 17 24 23 16 26 |
| user output |
|---|
| 0 8 16 18 11 17 24 23 16 26 |
Test 6
Verdict: ACCEPTED
| input |
|---|
| 100000 200000 18000 18001 426710313 73018 73012 558438094 87726 87671 355171790 53170 53171 869493690 ... |
| correct output |
|---|
| 0 479659405 1165315262 1854343... |
| user output |
|---|
| 0 479659405 1165315262 1854343... |
Test 7
Verdict: TIME LIMIT EXCEEDED
| input |
|---|
| 100000 200000 26504 26450 258578924 49543 49544 28958186 75174 75175 89459846 39175 39228 119699475 ... |
| correct output |
|---|
| 0 655556128 1413395076 1814086... |
| user output |
|---|
| (empty) |
Test 8
Verdict: TIME LIMIT EXCEEDED
| input |
|---|
| 100000 200000 39477 39413 773046299 69758 69759 558754983 23279 23280 142570619 61416 61479 874921013 ... |
| correct output |
|---|
| 0 269736525 626115013 70199222... |
| user output |
|---|
| (empty) |
Test 9
Verdict: TIME LIMIT EXCEEDED
| input |
|---|
| 100000 200000 76662 76636 844365635 73339 73342 755006676 89878 89879 396562588 18801 18781 954807004 ... |
| correct output |
|---|
| 0 598585836 1267139909 1803859... |
| user output |
|---|
| (empty) |
Test 10
Verdict: TIME LIMIT EXCEEDED
| input |
|---|
| 100000 200000 11724 11725 818399968 33244 33197 722525474 65530 65531 483965413 62405 62454 199581867 ... |
| correct output |
|---|
| 0 387990617 441010945 92441292... |
| user output |
|---|
| (empty) |
Test 11
Verdict: ACCEPTED
| input |
|---|
| 100000 200000 1 2 1 1 3 1 1 4 1 1 5 1 ... |
| correct output |
|---|
| 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ... |
| user output |
|---|
| 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ... |
Test 12
Verdict: ACCEPTED
| input |
|---|
| 100000 99999 1 2 1000000000 2 3 1000000000 3 4 1000000000 4 5 1000000000 ... |
| correct output |
|---|
| 0 1000000000 2000000000 300000... |
| user output |
|---|
| 0 1000000000 2000000000 300000... |
Test 13
Verdict: ACCEPTED
| input |
|---|
| 1 1 1 1 1 |
| correct output |
|---|
| 0 |
| user output |
|---|
| 0 |
Test 14
Verdict: ACCEPTED
| input |
|---|
| 99999 149997 1 2 1 2 3 1 3 4 1 4 5 1 ... |
| correct output |
|---|
| 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 ... |
| user output |
|---|
| 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 ... |
Test 15
Verdict: ACCEPTED
| input |
|---|
| 99997 149994 1 3 3 3 5 3 5 7 3 7 9 3 ... |
| correct output |
|---|
| 0 1 2 3 4 5 6 7 8 9 10 11 12 1... |
| user output |
|---|
| 0 1 2 3 4 5 6 7 8 9 10 11 12 1... |
Test 16
Verdict: ACCEPTED
| input |
|---|
| 60003 120000 1 2 30010 1 3 30010 1 4 30010 1 5 30010 ... |
| correct output |
|---|
| 0 30010 30010 30010 30010 3001... |
| user output |
|---|
| 0 30010 30010 30010 30010 3001... |
Test 17
Verdict: ACCEPTED
| input |
|---|
| 60003 120000 1 2 30010 1 3 30010 1 4 30010 1 5 30010 ... |
| correct output |
|---|
| 0 30010 30010 30010 30010 3001... |
| user output |
|---|
| 0 30010 30010 30010 30010 3001... |
Test 18
Verdict: ACCEPTED
| input |
|---|
| 100000 149997 1 50000 99997 1 49999 99995 1 49998 99993 1 49997 99991 ... |
| correct output |
|---|
| 0 1 3 5 7 9 11 13 15 17 19 21 ... |
| user output |
|---|
| 0 1 3 5 7 9 11 13 15 17 19 21 ... |
