結果

問題 No.788 トラックの移動
ユーザー MisterMister
提出日時 2020-08-05 16:12:25
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 507 ms / 2,000 ms
コード長 2,825 bytes
コンパイル時間 910 ms
コンパイル使用メモリ 95,708 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-21 17:35:43
合計ジャッジ時間 4,377 ms
ジャッジサーバーID
(参考情報)
judge11 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 477 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 112 ms
4,380 KB
testcase_05 AC 470 ms
4,376 KB
testcase_06 AC 478 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 1 ms
4,380 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 1 ms
4,376 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 141 ms
4,376 KB
testcase_16 AC 507 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <vector>
#include <tuple>
#include <limits>
#include <queue>

template <class T>
using MinHeap = std::priority_queue<T, std::vector<T>, std::greater<T>>;

template <class Cost = int>
struct Edge {
    int src, dst;
    Cost cost;
    Edge(int src = -1, int dst = -1, Cost cost = 1)
        : src(src), dst(dst), cost(cost){};

    bool operator<(const Edge<Cost>& e) const { return this->cost < e.cost; }
    bool operator>(const Edge<Cost>& e) const { return this->cost > e.cost; }
};

template <class Cost = int>
struct Graph {
    std::vector<std::vector<Edge<Cost>>> graph;

    Graph(int n = 0) : graph(n) {}

    void span(bool direct, int src, int dst, Cost cost = 1) {
        graph[src].emplace_back(src, dst, cost);
        if (!direct) graph[dst].emplace_back(dst, src, cost);
    }

    int size() const { return graph.size(); }
    void clear() { graph.clear(); }
    void resize(int n) { graph.resize(n); }

    std::vector<Edge<Cost>>& operator[](int v) { return graph[v]; }
    std::vector<Edge<Cost>> operator[](int v) const { return graph[v]; }
};

template <class Cost>
std::vector<Cost> dijkstra(const Graph<Cost>& graph, int s) {
    constexpr Cost INF = std::numeric_limits<Cost>::max();

    std::vector<Cost> dist(graph.size(), INF);
    dist[s] = 0;
    MinHeap<std::pair<Cost, int>> que;
    que.emplace(0, s);

    while (!que.empty()) {
        int v;
        Cost d;
        std::tie(d, v) = que.top();
        que.pop();
        if (d > dist[v]) continue;

        for (const auto& e : graph[v]) {
            if (dist[e.dst] <= dist[v] + e.cost) continue;
            dist[e.dst] = dist[v] + e.cost;
            que.emplace(dist[e.dst], e.dst);
        }
    }

    return dist;
}

using lint = long long;
constexpr lint INF = 1LL << 60;

void solve() {
    int n, m, r;
    std::cin >> n >> m >> r;
    --r;

    std::vector<lint> xs(n);
    for (auto& x : xs) std::cin >> x;

    if (std::count_if(xs.begin(), xs.end(),
                      [](auto a) { return a > 0; }) == 1) {
        std::cout << 0 << "\n";
        return;
    }

    Graph<lint> graph(n);
    while (m--) {
        int u, v;
        lint c;
        std::cin >> u >> v >> c;
        graph.span(false, --u, --v, c);
    }

    auto rds = dijkstra(graph, r);

    lint ans = INF;
    for (int c = 0; c < n; ++c) {
        auto ds = dijkstra(graph, c);

        lint cost = 0;
        for (int v = 0; v < n; ++v) cost += ds[v] * xs[v] * 2;

        lint red = 0;
        for (int v = 0; v < n; ++v) {
            red = std::max(red, ds[v] * std::min(xs[v], 1LL) - rds[v]);
        }

        ans = std::min(ans, cost - red);
    }

    std::cout << ans << "\n";
}

int main() {
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);

    solve();

    return 0;
}
0