Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c55a05a

Browse files
committed
feat: 94. 城市间货物运输 I SPEA新增js算法
1 parent e7c8d01 commit c55a05a

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

‎problems/kamacoder/0094.城市间货物运输I-SPFA.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /