結果

問題 No.788 トラックの移動
ユーザー PachicobuePachicobue
提出日時 2019-02-09 17:40:17
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 507 ms / 2,000 ms
コード長 2,352 bytes
コンパイル時間 936 ms
コンパイル使用メモリ 90,764 KB
実行使用メモリ 35,336 KB
最終ジャッジ日時 2023-08-21 17:30:00
合計ジャッジ時間 4,305 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 462 ms
35,048 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 106 ms
11,688 KB
testcase_05 AC 447 ms
35,072 KB
testcase_06 AC 464 ms
35,076 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,384 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 1 ms
4,376 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 118 ms
35,336 KB
testcase_16 AC 507 ms
35,236 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
using ll = long long;
#include <limits>
template <typename T>
constexpr T INF() { return std::numeric_limits<T>::max() / 16; }
#include <queue>
#include <vector>
template <typename T>
struct CostGraph
{
    CostGraph(const std::size_t v) : V{v}, edge(v), rev_edge(v) {}
    struct Edge
    {
        Edge(const std::size_t from, const std::size_t to, const T cost) : from{from}, to{to}, cost{cost} {}
        const std::size_t from, to;
        const T cost;
        bool operator<(const Edge& e) const { return cost != e.cost ? cost < e.cost : to < e.to; }
    };
    void addEdge(const std::size_t from, const std::size_t to, const T cost) { edge[from].push_back(Edge{from, to, cost}), rev_edge[to].push_back(Edge(to, from, cost)); }
    const std::size_t V;
    std::vector<std::vector<Edge>> edge, rev_edge;
};
template <typename T>
std::vector<T> Dijkstra(const CostGraph<T>& g, const std::size_t s)
{
    std::vector<T> d(g.V, INF<T>());
    using P = std::pair<T, std::size_t>;
    std::priority_queue<P, std::vector<P>, std::greater<P>> q;
    d[s] = 0, q.push({0, s});
    while (not q.empty()) {
        const T cost = q.top().first;
        const std::size_t v = q.top().second;
        q.pop();
        if (d[v] < cost) { continue; }
        for (const auto& e : g.edge[v]) {
            if (d[e.to] <= d[v] + e.cost) { continue; }
            d[e.to] = d[v] + e.cost, q.push({d[e.to], e.to});
        }
    }
    return d;
}
int main()
{
    int N, M, L;
    std::cin >> N >> M >> L, L--;
    CostGraph<ll> g(N);
    std::vector<ll> t(N);
    ll tsum = 0;
    for (int i = 0; i < N; i++) { std::cin >> t[i], tsum += t[i]; }
    for (int i = 0, a, b, c; i < M; i++) { std::cin >> a >> b >> c, a--, b--, g.addEdge(a, b, c), g.addEdge(b, a, c); }
    for (int i = 0; i < N; i++) {
        if (t[i] == tsum) { return std::cout << 0 << std::endl, 0; }
    }
    std::vector<std::vector<ll>> d(N, std::vector<ll>(N));
    for (int i = 0; i < N; i++) { d[i] = Dijkstra(g, i); }
    ll ans = INF<ll>();
    for (int i = 0; i < N; i++) {
        ll sum = 0;
        for (int j = 0; j < N; j++) { sum += 2 * d[i][j] * t[j]; }
        for (int j = 0; j < N; j++) {
            if (t[j] == 0) { continue; }
            ans = std::min(ans, sum + d[L][j] - d[i][j]);
        }
    }
    std::cout << ans << std::endl;
    return 0;
}
0