File tree Expand file tree Collapse file tree 1 file changed +54
-0
lines changed Expand file tree Collapse file tree 1 file changed +54
-0
lines changed Original file line number Diff line number Diff line change @@ -464,6 +464,60 @@ if __name__ == "__main__":
464464
465465### Javascript
466466
467+ ``` js
468+ async function main () {
469+ // 輸入
470+ const rl = require (' readline' ).createInterface ({ input: process .stdin })
471+ const iter = rl[Symbol .asyncIterator ]()
472+ const readline = async () => (await iter .next ()).value
473+ const [n , m ] = (await readline ()).split (" " ).map (Number )
474+ const grid = {}
475+ for (let i = 0 ; i < m ; i++ ) {
476+ const [src , desc , w ] = (await readline ()).split (" " ).map (Number )
477+ if (grid .hasOwnProperty (src)) {
478+ grid[src].push ([desc, w])
479+ } else {
480+ grid[src] = [[desc, w]]
481+ }
482+ }
483+ const minDist = Array .from ({length: n + 1 }, () => Number .MAX_VALUE )
484+
485+ // 起始點
486+ minDist[1 ] = 0
487+
488+ const q = [1 ]
489+ const visited = Array .from ({length: n + 1 }, () => false )
490+
491+ while (q .length ) {
492+ const src = q .shift ()
493+ const neighbors = grid[src]
494+ visited[src] = false
495+ if (neighbors) {
496+ for (const [desc , w ] of neighbors) {
497+ if (minDist[src] !== Number .MAX_VALUE
498+ && minDist[src] + w < minDist[desc]) {
499+ minDist[desc] = minDist[src] + w
500+ if (! visited[desc]) {
501+ q .push (desc)
502+ visited[desc] = true
503+ }
504+
505+ }
506+ }
507+ }
508+ }
509+
510+ // 輸出
511+ if (minDist[n] === Number .MAX_VALUE ) {
512+ console .log (' unconnected' )
513+ } else {
514+ console .log (minDist[n])
515+ }
516+ }
517+
518+ main ()
519+ ```
520+
467521### TypeScript
468522
469523### PhP
You can’t perform that action at this time.
0 commit comments