結果

問題 No.614 壊れたキャンパス
ユーザー legosuke
提出日時 2017-12-14 17:42:36
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
MLE  
実行時間 -
コード長 1,112 bytes
コンパイル時間 2,151 ms
コンパイル使用メモリ 177,976 KB
実行使用メモリ 814,764 KB
最終ジャッジ日時 2024-12-14 13:16:18
合計ジャッジ時間 24,684 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 11 WA * 3 TLE * 4 MLE * 2
権限があれば一括ダウンロードができます

ソースコード

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