結果

問題 No.788 トラックの移動
ユーザー merom686merom686
提出日時 2019-02-08 22:17:15
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 304 ms / 2,000 ms
コード長 2,050 bytes
コンパイル時間 1,026 ms
コンパイル使用メモリ 86,900 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-21 17:26:41
合計ジャッジ時間 3,330 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 302 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 70 ms
4,380 KB
testcase_05 AC 297 ms
4,376 KB
testcase_06 AC 304 ms
4,380 KB
testcase_07 AC 1 ms
4,380 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 2 ms
4,380 KB
testcase_12 AC 1 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 82 ms
4,376 KB
testcase_16 AC 229 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
using ll = long long;

struct Graph {
    struct VertexQ {
        bool operator<(const VertexQ& o) const {
            return c > o.c; // 逆
        }
        int i;
        ll c;
    };
    struct Vertex { int n; ll c; };
    struct Edge { int i, n, c; };
    Graph(int n, int m) : v(n, { -1 }), e(m), n(n), m(0) {}
    void add_edge(int i, int j, int c) {
        e[m] = { j, v[i].n, c };
        v[i].n = m;
        m++;
    }
    void dijkstra(int i) {
        for (int i = 0; i < n; i++) v[i].c = 1LL << 60;
        priority_queue<VertexQ> q;
        q.push({ i, v[i].c = 0 });
        while (!q.empty()) {
            auto p = q.top(); q.pop();
            if (p.c > v[p.i].c) continue;
            for (int j = v[p.i].n; j >= 0; j = e[j].n) {
                Edge& o = e[j];
                ll c = p.c + o.c;
                if (c < v[o.i].c) q.push({ o.i, v[o.i].c = c });
            }
        }
    }
    vector<Vertex> v;
    vector<Edge> e;
    int n, m;
};

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

    int n, m, l;
    cin >> n >> m >> l;
    l--;

    vector<int> t(n);
    for (int i = 0; i < n; i++) {
        cin >> t[i];
    }

    Graph g(n, m * 2);
    for (int i = 0; i < m; i++) {
        int a, b, c;
        cin >> a >> b >> c;
        a--; b--;

        g.add_edge(a, b, c);
        g.add_edge(b, a, c);
    }

    g.dijkstra(l);
    vector<ll> c(n);
    for (int i = 0; i < n; i++) {
        c[i] = g.v[i].c;
    }

    ll r = 1LL << 60;
    for (int i = 0; i < n; i++) {
        g.dijkstra(i);
        ll s = 0;
        ll x = 1LL << 60;
        for (int j = 0; j < n; j++) {
            if (j == i) continue;
            if (t[j] > 0) x = min(x, c[j] - g.v[j].c);
            s += g.v[j].c * t[j];
        }
        s *= 2;
        if (x < 1LL << 60) s += x;
        r = min(r, s);
    }

    cout << r << endl;

    return 0;
}
0