結果

問題 No.2739 Time is money
ユーザー tobbietobbie
提出日時 2024-04-24 07:46:39
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 376 ms / 2,000 ms
コード長 1,740 bytes
コンパイル時間 1,732 ms
コンパイル使用メモリ 174,848 KB
実行使用メモリ 19,072 KB
最終ジャッジ日時 2024-04-24 07:46:50
合計ジャッジ時間 9,738 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 158 ms
12,928 KB
testcase_03 AC 305 ms
16,080 KB
testcase_04 AC 171 ms
12,288 KB
testcase_05 AC 195 ms
12,032 KB
testcase_06 AC 268 ms
14,328 KB
testcase_07 AC 344 ms
18,816 KB
testcase_08 AC 370 ms
19,072 KB
testcase_09 AC 348 ms
19,072 KB
testcase_10 AC 291 ms
18,560 KB
testcase_11 AC 376 ms
18,944 KB
testcase_12 AC 307 ms
16,512 KB
testcase_13 AC 313 ms
16,640 KB
testcase_14 AC 266 ms
16,640 KB
testcase_15 AC 223 ms
15,424 KB
testcase_16 AC 237 ms
15,364 KB
testcase_17 AC 311 ms
19,072 KB
testcase_18 AC 347 ms
19,072 KB
testcase_19 AC 258 ms
15,428 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using ll = long long int;
#define INF 1e18+7

struct Edge {
  int to;
  int c;
  int t;
  Edge(int to, int c, int t) : to(to), c(c), t(t) {}
};

vector<int> dijkstra(int N, vector<vector<Edge>> &graph, int X) {
  vector<double> distance(N, INF);
  vector<int> from(N, -1);
  priority_queue<pair<double, int>,
                 vector<pair<double, int>>,
                 greater<pair<double, int>>> q;
  distance[0] = 0;
  q.push({0, 0});
  while (!q.empty()) {
    pair<double, int> p = q.top();
    q.pop();
    double path_now = p.first;
    int p_now = p.second;
    if (distance[p_now] < path_now)
      continue;
    for (Edge e : graph[p_now]) {
      double path_new = path_now + (double)e.c/X + (double)e.t;
      int p_new = e.to;
      if (path_new < distance[p_new]) {
	distance[p_new] = path_new;
	from[p_new] = p_now;
	q.push({path_new, p_new});
      }
    }
  }
  //return distance;
  return from;
}

int main() {
  int N, M, X;
  cin >> N >> M >> X;
  vector<vector<Edge>> g(N);
  rep(i, M) {
    int u, v, C, T;
    cin >> u >> v >> C >> T;
    u--; v--;
    g[u].push_back({v, C, T});
    g[v].push_back({u, C, T});
  }
  //vector<double> s = dijkstra(N, g);
  vector<int> s = dijkstra(N, g, X);
  //if (s[N-1] < INF)
  //  cout << (ll)((s[N-1]*X + (X-1))/X) << endl;
  //else
  //  cout << -1 << endl;
  ll time = 0;
  ll cost = 0;
  int d = N - 1;
  while (d != -1) {
    rep(i, (int)g[d].size()) {
      if (g[d][i].to == s[d]) {
	cost += g[d][i].c;
	time += g[d][i].t;
	break;
      }
    }
    d = s[d];
  }
  if (time == 0)
    cout << -1 << endl;
  else
    cout << time + (cost + (X-1)) / X << endl;
  return 0;
}
0