AI Nativeレベル別課題

レベル3 · 応用 · 構造と効率

最小コスト経路

有向グラフで到達可能な最小コストを求めます。

問題文

function solution(nodes, edges, start)を書いてください。入出力はJSON値です。入力配列やオブジェクトを変更しないでください。外部ライブラリ・ネットワーク・非同期関数は使えません。 nodesは異なる文字列の配列でstartを含みます。edgesは有向辺{from:string,to:string,cost:非負整数}の配列です。不明ノードを含む辺は無視します。startから各ノードへの最小コストを求め、nodesの入力順に{id,distance}を返してください。startは0、到達不能はnullです。コスト0、重複辺、自己ループも有効で、経路コストの合計は安全な整数範囲です。 入力範囲:各入力配列は最大50,000項目、文字列は最大1,000文字です。数値は有限値で、整数と整数の合算結果はJavaScriptの安全な整数範囲内です。その他の型と有効性は上記の規則に従います。

入出力例

例 1

関数の引数

[
  [
    "a",
    "b",
    "c"
  ],
  [
    {
      "from": "a",
      "to": "b",
      "cost": 2
    },
    {
      "from": "b",
      "to": "c",
      "cost": 3
    }
  ],
  "a"
]

戻り値

[
  {
    "id": "a",
    "distance": 0
  },
  {
    "id": "b",
    "distance": 2
  },
  {
    "id": "c",
    "distance": 5
  }
]
例 2

関数の引数

[
  [
    "a",
    "b"
  ],
  [],
  "a"
]

戻り値

[
  {
    "id": "a",
    "distance": 0
  },
  {
    "id": "b",
    "distance": null
  }
]
例 3

関数の引数

[
  [
    "a",
    "b"
  ],
  [
    {
      "from": "x",
      "to": "b",
      "cost": 1
    },
    {
      "from": "a",
      "to": "x",
      "cost": 1
    }
  ],
  "a"
]

戻り値

[
  {
    "id": "a",
    "distance": 0
  },
  {
    "id": "b",
    "distance": null
  }
]
Practice has no points, deadline or submission cap. Use AI and search freely. Sign in to run public examples, or verify locally.

78 / 20,000文字

Add your test · optional

Enter a JSON argument array in nodes, edges, start order. Your test runs alongside the three examples.

Save both files in one folder and run this command with Node.js. Read the checks field for individual example results.

node verify-shortest-routes.mjs
문의하기

서비스명: 포트포올
문의는 운영자에게 이메일로 전송됩니다.

이메일과 문의 내용은 답변을 위해 운영자 및 메일 발송 서비스로 전송됩니다. 비밀번호·결제정보·민감한 개인정보는 입력하지 마세요.