結果

問題 No.788 トラックの移動
ユーザー 👑 emthrmemthrm
提出日時 2019-02-12 02:50:14
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 496 ms / 2,000 ms
コード長 3,554 bytes
コンパイル時間 1,471 ms
コンパイル使用メモリ 138,880 KB
実行使用メモリ 34,812 KB
最終ジャッジ日時 2023-08-21 17:30:27
合計ジャッジ時間 4,827 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 462 ms
34,740 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 104 ms
10,920 KB
testcase_05 AC 454 ms
34,436 KB
testcase_06 AC 463 ms
34,812 KB
testcase_07 AC 1 ms
4,380 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,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 136 ms
34,424 KB
testcase_16 AC 496 ms
34,732 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>
using namespace std;

#define FOR(i,m,n) for(int i=(m);i<(n);++i)
#define REP(i,n) FOR(i,0,n)
#define ALL(v) (v).begin(),(v).end()

const int INF = 0x3f3f3f3f;
const long long LINF = 0x3f3f3f3f3f3f3f3fLL;
/*----------------------------------------*/
template <typename T, const T TINF>
struct Dijkstra {
  struct Edge {
    int src, dst;
    T cost;
    Edge(int dst_, T cost_ = 0) : src(-1), dst(dst_), cost(cost_) {}
    Edge(int src_, int dst_, T cost_) : src(src_), dst(dst_), cost(cost_) {}
    inline bool operator < (const Edge &rhs) { return cost < rhs.cost; }
    inline bool operator <= (const Edge &rhs) { return cost <= rhs.cost; }
    inline bool operator > (const Edge &rhs) { return cost > rhs.cost; }
    inline bool operator >= (const Edge &rhs) { return cost >= rhs.cost; }
  };

  vector<vector<Edge> > graph;
  vector<T> dist;

  Dijkstra(int sz_) : sz(sz_), graph(sz_), dist(sz_, TINF), prev(sz_, -1) {}

  void add(int src, int dst, T cost) { graph[src].emplace_back(Edge(dst, cost)); }

  void build(int s) {
    dist.assign(sz, TINF);
    prev.assign(sz, -1);
    dist[s] = 0;
    priority_queue<pair<T, int>, vector<pair<T, int> >, greater<pair<T, int> > > que;
    que.emplace(0, s);
    while (!que.empty()) {
      pair<T, int> pr = que.top(); que.pop();
      int ver = pr.second;
      if (dist[ver] < pr.first) continue;
      for (Edge e : graph[ver]) {
        if (dist[e.dst] > dist[ver] + e.cost) {
          dist[e.dst] = dist[ver] + e.cost;
          prev[e.dst] = ver;
          que.emplace(dist[e.dst], e.dst);
        }
      }
    }
  }

  vector<int> build_path(int t) {
    vector<int> res;
    for (; t != -1; t = prev[t]) res.emplace_back(t);
    reverse(ALL(res));
    return res;
  }

private:
  int sz;
  vector<int> prev;
};
// Dijkstra<costの型, LINF> dij(sz) = 頂点数 sz の単一始点最短路を考える
// dij.graph = グラフ
// dij.dist[ver] = 始点から頂点 ver までの最短距離
// dij.add(src, dst, cost) = 始点 src, 終点 dst(, 重み cost)の辺をグラフに追加する
// dij.build(s) = 始点 start の単一始点最短路を求める
// dij.build_path(t) = 終点 t の最短路を求める

int main() {
  cin.tie(0); ios::sync_with_stdio(false);
  // freopen("input.txt", "r", stdin);

  int n, m, l; cin >> n >> m >> l; --l;
  vector<int> t(n);
  int corner_case = 0;
  REP(i, n) {
    cin >> t[i];
    if (t[i] > 0) ++corner_case;
  }
  if (corner_case <= 1) {
    cout << 0 << '\n';
    return 0;
  }
  Dijkstra<long long, LINF> dij(n);
  REP(i, m) {
    int a, b; cin >> a >> b; --a; --b;
    long long c; cin >> c;
    dij.add(a, b, c);
    dij.add(b, a, c);
  }
  vector<vector<long long> > d(n, vector<long long>(n, LINF));
  REP(i, n) {
    dij.build(i);
    REP(j, n) d[i][j] = dij.dist[j];
  }
  long long ans = LINF;
  REP(i, n) { // 集める交差点
    long long tmp = 0;
    REP(j, n) tmp += 2 * d[i][j] * t[j];
    REP(j, n) ans = min(ans, tmp + d[l][j] + d[j][i] - (t[j] > 0 ? d[j][i] * 2 : 0));
  }
  cout << ans << '\n';
  return 0;
}
0