mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-06 15:09:40 +08:00
Merge pull request #2761 from suinming/kamacoder-94-js
feat: 94. 城市间货物运输 I 新增js算法
This commit is contained in:
@ -485,6 +485,45 @@ if __name__ == "__main__":
|
|||||||
|
|
||||||
### Javascript
|
### Javascript
|
||||||
|
|
||||||
|
```js
|
||||||
|
async function main() {
|
||||||
|
// 輸入
|
||||||
|
const rl = require('readline').createInterface({ input: process.stdin })
|
||||||
|
const iter = rl[Symbol.asyncIterator]()
|
||||||
|
const readline = async () => (await iter.next()).value
|
||||||
|
const [n, m] = (await readline()).split(" ").map(Number)
|
||||||
|
const edges = []
|
||||||
|
for (let i = 0 ; i < m ; i++) {
|
||||||
|
edges.push((await readline()).split(" ").map(Number))
|
||||||
|
}
|
||||||
|
const minDist = Array.from({length: n + 1}, () => Number.MAX_VALUE)
|
||||||
|
// 起始點
|
||||||
|
minDist[1] = 0
|
||||||
|
|
||||||
|
for (let i = 1 ; i < n ; i++) {
|
||||||
|
let update = false
|
||||||
|
for (const [src, desc, w] of edges) {
|
||||||
|
if (minDist[src] !== Number.MAX_VALUE && minDist[src] + w < minDist[desc]) {
|
||||||
|
minDist[desc] = minDist[src] + w
|
||||||
|
update = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!update) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 輸出
|
||||||
|
if (minDist[n] === Number.MAX_VALUE) {
|
||||||
|
console.log('unconnected')
|
||||||
|
} else {
|
||||||
|
console.log(minDist[n])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
main()
|
||||||
|
```
|
||||||
|
|
||||||
### TypeScript
|
### TypeScript
|
||||||
|
|
||||||
### PhP
|
### PhP
|
||||||
|
Reference in New Issue
Block a user