結果

問題 No.614 壊れたキャンパス
ユーザー Kutimoti_TKutimoti_T
提出日時 2018-06-24 14:08:43
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 1,836 bytes
コンパイル時間 1,617 ms
コンパイル使用メモリ 170,332 KB
実行使用メモリ 25,964 KB
最終ジャッジ日時 2023-09-13 13:38:56
合計ジャッジ時間 5,906 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 WA -
testcase_09 AC 526 ms
25,964 KB
testcase_10 AC 256 ms
15,124 KB
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 174 ms
12,836 KB
testcase_16 WA -
testcase_17 AC 241 ms
16,428 KB
testcase_18 AC 199 ms
17,452 KB
testcase_19 AC 259 ms
22,856 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define rep(i,s,e) for(int (i) = (s);(i) <= (e);(i)++)

int N,M,K,S,T;

vector<int> A,B,C;

map<i64,i64> dp;
int main(){
  cin >> N >> M >> K >> S >> T;
  A.resize(M);
  B.resize(M);
  C.resize(M);
  vector<vector<pair<i64,i64>>> vec(N);
  rep(i,0,M - 1){
    cin >> C[i] >> A[i] >> B[i];
    C[i]--;
    vec[C[i]].push_back({B[i],A[i]});
  }
  vec[N - 1].push_back({-1,T});
  rep(i,0,N - 2){
    sort(vec[i].begin(),vec[i].end());
  }

  for(auto p : vec[0]){
    dp[p.second] = abs(S - p.second);
  }

  rep(c,1,N - 1){
    auto & before = vec[c - 1];
    auto & next = vec[c];
    map<i64,i64> dp2;
    vector<i64> up_min;
    vector<i64> bot_min;
    for(auto p : before){
      i64 a = p.second;
      i64 b = p.first;
      if(up_min.empty()){
        up_min.push_back(dp[a] - b);
      }
      else{
        up_min.push_back(min(up_min.back(),dp[a] - b));
      }
    }
    reverse(before.begin(),before.end());

    for(auto p : before){
      i64 a = p.second;
      i64 b = p.first;
      if(bot_min.empty()){
        bot_min.push_back(dp[a] + b);
      }
      else{
        bot_min.push_back(min(bot_min.back(),dp[a] + b));
      }
    }

    reverse(before.begin(),before.end());

    int cnt = 0;

    if(before.empty()){
      cout << -1 << endl;
      return 0;
    }

    for(auto p : next){
      i64 s = p.second;
      while(cnt < before.size() && s > before[cnt].first){
        cnt++;
      }
      i64 MIN;
      if(cnt == 0){
        MIN = bot_min[bot_min.size() - cnt - 1] - s;
      }
      else if(cnt == bot_min.size()){
        MIN = up_min[cnt - 1] + s;
      }
      else{
        MIN = min(bot_min[bot_min.size() - cnt - 1] - s, up_min[cnt - 1] + s);
      }
      dp2[s] = MIN;
    }
    swap(dp2,dp);
  }

  cout << dp[T] << endl;
}
0