結果

問題 No.614 壊れたキャンパス
ユーザー Kutimoti_TKutimoti_T
提出日時 2018-06-24 14:34:33
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 517 ms / 2,000 ms
コード長 1,965 bytes
コンパイル時間 2,028 ms
コンパイル使用メモリ 166,668 KB
実行使用メモリ 51,456 KB
最終ジャッジ日時 2023-09-13 13:40:51
合計ジャッジ時間 7,396 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
12,916 KB
testcase_01 AC 6 ms
12,808 KB
testcase_02 AC 6 ms
12,800 KB
testcase_03 AC 6 ms
13,116 KB
testcase_04 AC 6 ms
12,848 KB
testcase_05 AC 6 ms
12,844 KB
testcase_06 AC 6 ms
12,996 KB
testcase_07 AC 6 ms
12,892 KB
testcase_08 AC 234 ms
39,028 KB
testcase_09 AC 517 ms
41,504 KB
testcase_10 AC 306 ms
36,416 KB
testcase_11 AC 234 ms
38,200 KB
testcase_12 AC 293 ms
38,148 KB
testcase_13 AC 248 ms
38,304 KB
testcase_14 AC 263 ms
39,252 KB
testcase_15 AC 212 ms
31,592 KB
testcase_16 AC 259 ms
40,692 KB
testcase_17 AC 279 ms
51,456 KB
testcase_18 AC 212 ms
32,684 KB
testcase_19 AC 281 ms
39,036 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

i64 N,M,K,S,T;

vector<i64> A,B,C;

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

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

  rep(c,1,N - 1){
    auto & before = vec[c - 1];
    auto & next = vec2[c];
    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[c - 1][a] - b);
      }
      else{
        up_min.push_back(min(up_min.back(),dp[c - 1][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[c - 1][a] + b);
      }
      else{
        bot_min.push_back(min(bot_min.back(),dp[c - 1][a] + b));
      }
    }

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

    i64 cnt = 0;

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

    for(auto p : next){
      i64 s = p.first;
      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);
      }
      dp[c][s] = MIN;
    }
  }

  cout << dp[N - 1][T] << endl;
}
0