結果
| 問題 | No.788 トラックの移動 | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2019-02-08 22:22:00 | 
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 486 ms / 2,000 ms | 
| コード長 | 2,406 bytes | 
| コンパイル時間 | 1,994 ms | 
| コンパイル使用メモリ | 208,192 KB | 
| 最終ジャッジ日時 | 2025-01-06 21:00:01 | 
| ジャッジサーバーID (参考情報) | judge5 / judge4 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 14 | 
ソースコード
#include <bits/stdc++.h>
using ll = long long;
template <typename T>
constexpr T MOD() { return 1000000007; }
template <typename T>
constexpr T INF() { return std::numeric_limits<T>::max() / 2; }
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 sum = 0;
    for (int i = 0; i < N; i++) { std::cin >> t[i], sum += t[i]; }
    for (int i = 0; i < N; i++) {
        if (sum == t[i]) { return std::cout << 0 << std::endl, 0; }
    }
    for (int i = 0; i < M; i++) {
        int a, b;
        ll c;
        std::cin >> a >> b >> c, a--, b--;
        g.addEdge(a, b, c), g.addEdge(b, a, c);
    }
    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 * t[j] * d[j][i]; }
        for (int j = 0; j < N; j++) {
            if (t[j] == 0) { continue; }
            ans = std::min(ans, sum - d[i][j] + d[L][j]);
        }
    }
    std::cout << ans << std::endl;
    return 0;
}
            
            
            
        