結果

問題 No.614 壊れたキャンパス
ユーザー pekempeypekempey
提出日時 2017-12-14 00:21:35
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,212 ms / 2,000 ms
コード長 1,827 bytes
コンパイル時間 1,343 ms
コンパイル使用メモリ 104,596 KB
実行使用メモリ 75,516 KB
最終ジャッジ日時 2023-08-21 02:31:54
合計ジャッジ時間 14,347 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1,119 ms
73,476 KB
testcase_09 AC 1,194 ms
70,872 KB
testcase_10 AC 1,054 ms
75,516 KB
testcase_11 AC 1,164 ms
74,188 KB
testcase_12 AC 1,212 ms
73,744 KB
testcase_13 AC 1,161 ms
74,264 KB
testcase_14 AC 1,122 ms
74,276 KB
testcase_15 AC 984 ms
73,504 KB
testcase_16 AC 1,027 ms
73,248 KB
testcase_17 AC 525 ms
73,408 KB
testcase_18 AC 514 ms
66,844 KB
testcase_19 AC 569 ms
63,196 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <queue>

using namespace std;

struct Edge {
  int v;
  long long w;
};

std::vector<long long> dijkstra(const std::vector<std::vector<Edge>> &g, int s) {
  const int n = g.size();
  std::vector<long long> dist(n, 1e18);
  std::priority_queue<std::pair<long long, int>> q;
  q.emplace(0, s);
  dist[s] = 0;
  while (!q.empty()) {
    long long d = -q.top().first;
    int u = q.top().second;
    q.pop();
    if (d > dist[u]) continue;
    for (Edge e : g[u]) {
      if (dist[e.v] > dist[u] + e.w) {
        dist[e.v] = dist[u] + e.w;
        q.emplace(-dist[e.v], e.v);
      }
    }
  }
  return dist;
}

int main() {
  int n, m, k, s, t;
  cin >> n >> m >> k >> s >> t;

  map<pair<int, int>, int> mp;
  mp[{1, s}];
  mp[{n, t}];
  vector<int> a(m), b(m), c(m);
  vector<vector<int>> g(n + 1);
  g[1].push_back(s);
  g[n].push_back(t);
  for (int i = 0; i < m; i++) {
    cin >> a[i] >> b[i] >> c[i];
    mp[{a[i], b[i]}];
    mp[{a[i] + 1, c[i]}];
    g[a[i]].push_back(b[i]);
    g[a[i] + 1].push_back(c[i]);
  }

  int ii = 0;
  for (auto &kv : mp) {
    kv.second = ii++;
  }

  vector<vector<Edge>> gg(mp.size());
  for (int i = 1; i <= n; i++) {
    sort(g[i].begin(), g[i].end());
    for (int j = 0; j + 1 < g[i].size(); j++) {
      int u = g[i][j];
      int v = g[i][j + 1];
      int uu = mp[{i, u}];
      int vv = mp[{i, v}];

      gg[uu].push_back({vv, v - u});
      gg[vv].push_back({uu, v - u});
    }
  }

  for (int i = 0; i < m; i++) {
    int u = mp[{a[i], b[i]}];
    int v = mp[{a[i] + 1, c[i]}];
    gg[u].push_back({v, 0});
  }

  int start = mp[{1, s}];
  int end = mp[{n, t}];
  auto dist = dijkstra(gg, start);
  if (dist[end] == 1e18) {
    cout << -1 << endl;
  } else {
    cout << dist[end] << endl;
  }
}
0