結果

問題 No.788 トラックの移動
ユーザー ei1333333ei1333333
提出日時 2019-02-08 21:43:57
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 442 ms / 2,000 ms
コード長 2,033 bytes
コンパイル時間 2,391 ms
コンパイル使用メモリ 214,436 KB
実行使用メモリ 34,932 KB
最終ジャッジ日時 2023-08-21 17:26:07
合計ジャッジ時間 5,540 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 400 ms
34,812 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 95 ms
11,340 KB
testcase_05 AC 392 ms
34,808 KB
testcase_06 AC 399 ms
34,884 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 85 ms
34,852 KB
testcase_16 AC 442 ms
34,932 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

using int64 = long long;
const int64 INF = 1LL << 60;

template< typename T >
struct edge {
  int src, to;
  T cost;

  edge(int to, T cost) : src(-1), to(to), cost(cost) {}

  edge(int src, int to, T cost) : src(src), to(to), cost(cost) {}

  edge &operator=(const int &x) {
    to = x;
    return *this;
  }

  operator int() const { return to; }
};

template< typename T >
using Edges = vector< edge< T > >;
template< typename T >
using WeightedGraph = vector< Edges< T > >;
using UnWeightedGraph = vector< vector< int > >;
template< typename T >
using Matrix = vector< vector< T > >;

template< typename T >
vector< T > dijkstra(WeightedGraph< T > &g, int s) {
  const auto INF = numeric_limits< T >::max();
  vector< T > dist(g.size(), INF);

  using Pi = pair< T, int >;
  priority_queue< Pi, vector< Pi >, greater< Pi > > que;
  dist[s] = 0;
  que.emplace(dist[s], s);
  while(!que.empty()) {
    T cost;
    int idx;
    tie(cost, idx) = que.top();
    que.pop();
    if(dist[idx] < cost) continue;
    for(auto &e : g[idx]) {
      auto next_cost = cost + e.cost;
      if(dist[e.to] <= next_cost) continue;
      dist[e.to] = next_cost;
      que.emplace(dist[e.to], e.to);
    }
  }
  return dist;
}


int main() {
  int N, M, L;
  vector< int64 > v[2000];
  cin >> N >> M >> L;
  --L;
  WeightedGraph< int64 > g(N);
  vector< pair< int, int > > ex;
  for(int i = 0; i < N; i++) {
    int p;
    cin >> p;
    if(p > 0) ex.emplace_back(i, p);
  }
  for(int i = 0; i < M; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    --a, --b;
    g[a].emplace_back(b, c);
    g[b].emplace_back(a, c);
  }
  for(int k = 0; k < N; k++) {
    v[k] = dijkstra(g, k);
  }
  int64 ret = INF;
  for(int i = 0; i < N; i++) {
    int64 cost = 0, pv = 0;
    bool exist = false;
    for(auto e : ex) {
      cost += v[i][e.first] * 2 * e.second;
    }
    for(auto e : ex) {
      pv = max(pv, v[i][e.first] - v[L][e.first]);
    }

    ret = min(ret, cost - pv);
  }
  cout << ret << endl;
}

0