結果

問題 No.614 壊れたキャンパス
ユーザー legosukelegosuke
提出日時 2017-12-14 17:42:36
言語 C++11
(gcc 11.4.0)
結果
MLE  
実行時間 -
コード長 1,112 bytes
コンパイル時間 2,639 ms
コンパイル使用メモリ 164,988 KB
実行使用メモリ 814,396 KB
最終ジャッジ日時 2023-08-21 04:34:18
合計ジャッジ時間 4,272 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
8,024 KB
testcase_01 AC 4 ms
8,024 KB
testcase_02 AC 4 ms
8,020 KB
testcase_03 AC 4 ms
8,012 KB
testcase_04 AC 4 ms
8,112 KB
testcase_05 MLE -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
typedef pair<int, int> pii;

map<pii, ll> mp;
map<pii, pii> pre;
int n, m, k, s, t;
vector<int> v[200010];

ll rec(pii x) {
  if (mp[x]) return mp[x];
  ll res = 1LL << 60;
  for (int i = 0; i < v[x.first].size(); i++) {
    pii y = pii(x.first, v[x.first][i]);
    res = min(res, rec(pre[y]) + abs(x.second - v[x.first][i]));
  }
  return mp[x] = res;
}

int main(void) {
  cin >> n >> m >> k >> s >> t;
  if (m == 0) {
    if (n == 1) {
      cout << abs(s - t) << endl;
    } else {
      cout << -1 << endl;
    }
  } else {
    for (int i = 0; i < m; i++) {
      int a, b, c;
      cin >> a >> b >> c;
      v[a].push_back(b);
      v[a + 1].push_back(c);
      pre[pii(a + 1, c)] = pii(a, b);
    }
    for (int i = 1; i <= n; i++) {
      sort(v[i].begin(), v[i].end());
    }
    for (int i = 0; i < v[1].size(); i++) {
      pre[pii(1, v[1][i])] = pii(1, s);
      mp[pii(1, v[1][i])] = abs(v[1][i] - s);
    }

    ll ans = 1LL << 60;
    ans = rec(pii(n, t));
    cout << (ans == 1LL << 60 ? -1 : ans) << endl;
  }
  
  return 0;
}
0