// An implementation of // https://sampleprograms.io/projects/dijkstra/ val shortestPath = { !sys.aryi64 matrix, int nodes, int src, int dst -> i64 infinity = (1 << 63)-1; var dist = new i64[nodes]; var visited = new sys.adt.bitset(); // All nodes are infinitely far from each other for( int i=0; i 0 && !visited.get(node) && best+weight < dist[node] ) dist[node] = best+weight; } } count++; } return result; }; val usage = { -> sys.io.p("Usage: please provide three inputs: a serialized matrix, a source node and a destination node\n"); return 0; }; val main = { u8[~][] args -> // Exactly 3 args if( args# != 4 ) return usage(); // First is a serialized square matrix of positive numbers var matrix = sys.aryi64.parseAryI64(args[1]); if( !matrix ) return usage(); if( !matrix.len ) return usage(); // All numbers are positive for( int i=0; i= nodes || dst < 0 || dst >= nodes ) return usage(); // Source and destination land within the graph i64 cost = shortestPath(matrix,nodes,src,dst); if( cost >= 0 ) { new sys.aryu8().num(cost).add('\n').write(1); return 0; } return usage(); };