結果

問題 No.1 道のショートカット
ユーザー alpha_virginisalpha_virginis
提出日時 2016-08-08 23:40:14
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 12 ms / 5,000 ms
コード長 2,303 bytes
コンパイル時間 1,431 ms
コンパイル使用メモリ 176,596 KB
実行使用メモリ 7,432 KB
最終ジャッジ日時 2023-09-27 21:57:49
合計ジャッジ時間 2,922 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,432 KB
testcase_01 AC 2 ms
4,440 KB
testcase_02 AC 2 ms
4,420 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 1 ms
4,384 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,448 KB
testcase_07 AC 2 ms
4,408 KB
testcase_08 AC 8 ms
6,752 KB
testcase_09 AC 5 ms
5,764 KB
testcase_10 AC 4 ms
5,132 KB
testcase_11 AC 5 ms
5,620 KB
testcase_12 AC 12 ms
7,428 KB
testcase_13 AC 11 ms
7,432 KB
testcase_14 AC 2 ms
4,668 KB
testcase_15 AC 2 ms
4,500 KB
testcase_16 AC 4 ms
4,900 KB
testcase_17 AC 1 ms
4,496 KB
testcase_18 AC 2 ms
4,568 KB
testcase_19 AC 4 ms
4,844 KB
testcase_20 AC 4 ms
5,056 KB
testcase_21 AC 4 ms
5,220 KB
testcase_22 AC 2 ms
4,728 KB
testcase_23 AC 7 ms
6,776 KB
testcase_24 AC 9 ms
6,876 KB
testcase_25 AC 4 ms
5,340 KB
testcase_26 AC 4 ms
5,016 KB
testcase_27 AC 8 ms
6,472 KB
testcase_28 AC 2 ms
4,584 KB
testcase_29 AC 3 ms
4,800 KB
testcase_30 AC 2 ms
4,496 KB
testcase_31 AC 3 ms
4,884 KB
testcase_32 AC 7 ms
5,980 KB
testcase_33 AC 5 ms
5,508 KB
testcase_34 AC 6 ms
6,264 KB
testcase_35 AC 2 ms
4,488 KB
testcase_36 AC 3 ms
4,740 KB
testcase_37 AC 2 ms
4,460 KB
testcase_38 AC 2 ms
4,484 KB
testcase_39 AC 3 ms
4,872 KB
testcase_40 AC 2 ms
4,596 KB
testcase_41 AC 2 ms
4,420 KB
testcase_42 AC 2 ms
4,380 KB
testcase_43 AC 1 ms
4,516 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

template<typename T> T in() { abort(); return T(); }
template<> std::string in() { std::string str; std::cin >> str; return str; }
template<> int in() { int x; scanf("%d", &x); return x; }

template<typename T> void out(T x) { abort(); }
template<> void out(const char* x) { printf("%s\n", x); }
template<> void out(std::string x) { std::cout << x << std::endl; }
template<> void out(int x) { printf("%d\n", x); }
template<> void out(long x) { printf("%ld\n", x); }

class Edge {
public:
  Edge() {}
  Edge(int to_, int time_) : to(to_), time(time_) {}
  int to;
  int time;
};

std::vector< Edge > edges[64 * 512];
int buf[64 * 512];
int xs[64 * 512];
int buf2[64 * 512];

inline int nodeId(int id, int cost) {
  int res = (id << 9) | cost;
  assert( 0 <= res and res < 64 * 512 );
  return res;
}

constexpr int inf = (1 << 28);

int main() {
  for(int i = 0; i < 64 * 512; ++i) {
    xs[i] = inf;
  }
  for(int i = 0; i < 512; ++i) {
    xs[nodeId(1, i)] = 0;
  }
       
  auto n = in<int>();
  auto c = in<int>();
  auto v = in<int>();
  auto ss = std::vector<int>(v, 0);
  for(auto& x : ss) x = in<int>();
  auto ts = std::vector<int>(v, 0);
  for(auto& x : ts) x = in<int>();
  auto ys = std::vector<int>(v, 0);
  for(auto& x : ys) x = in<int>();
  auto ms = std::vector<int>(v, 0);
  for(auto& x : ms) x = in<int>();
  for(int i = 0; i < v; ++i) {
    int from = ss[i];
    int to   = ts[i];
    int cost = ys[i];
    int time = ms[i];
    for(int j = 0; j <= c; ++j) {
      int nc = j + cost;
      if( c < nc ) break; 
      edges[nodeId(from, j)].push_back( Edge(nodeId(to, nc), time) );
    }
  }
  
  std::priority_queue< std::pair<int, int> > q;
  for(int i = 0; i <= c; ++i) {
    q.push(std::pair<int, int>(0, nodeId(1, i)));
  }
  while( not q.empty() ) {
    int id, time;
    std::tie(time, id) = q.top(); q.pop();
    time = -time;
    for(int i = 0; i < (int)edges[id].size(); ++i) {
      Edge& edge = edges[id][i];
      int to = edge.to;
      int ntime = time + edge.time;
      if( xs[to] <= ntime ) continue;
      xs[to] = ntime;
      q.push( std::pair<int, int>(-ntime, to) );
    }
  }
  
  int res = inf;
  for(int i = 0; i <= c; ++i) {
    res = std::min(res, xs[nodeId(n, i)]);
  }
  printf("%d\n", res == inf ? -1 : res);
 
  
  return 0;
}
0