File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ const input = require ( 'fs' )
2+ . readFileSync ( process . platform === 'linux' ? '/dev/stdin' : './input.txt' )
3+ . toString ( )
4+ . trim ( )
5+ . split ( '\n' )
6+ . map ( ( el ) => el . split ( ' ' ) . map ( Number ) ) ;
7+
8+ function solution ( input ) {
9+ const n = input [ 0 ] [ 0 ] ;
10+ const m = input [ 0 ] [ 1 ] ;
11+ const busInfo = input . slice ( 2 ) ;
12+ const map = Array . from ( { length : n } , ( ) => Array ( n ) . fill ( Infinity ) ) ;
13+ for ( let i = 0 ; i < n ; i ++ ) {
14+ map [ i ] [ i ] = 0 ;
15+ }
16+ for ( const [ start , end , cost ] of busInfo ) {
17+ map [ start - 1 ] [ end - 1 ] = Math . min ( map [ start - 1 ] [ end - 1 ] , cost ) ;
18+ }
19+
20+ function backtrack ( ) { }
21+
22+ for ( let i = 0 ; i < n ; i ++ ) {
23+ for ( let j = 0 ; j < n ; j ++ ) {
24+ for ( let k = 0 ; k < n ; k ++ ) {
25+ if ( i === j ) continue ;
26+ if ( map [ i ] [ j ] > map [ i ] [ k ] + map [ k ] [ j ] ) {
27+ map [ i ] [ j ] = map [ i ] [ k ] + map [ k ] [ j ] ;
28+ }
29+ }
30+ }
31+ }
32+ }
33+
34+ console . log ( solution ( input ) ) ;
You can’t perform that action at this time.
0 commit comments