結果

問題 No.788 トラックの移動
コンテスト
ユーザー 梧桐
提出日時 2026-01-27 00:52:57
言語 C++17
(gcc 15.2.0 + boost 1.89.0)
結果
AC  
実行時間 442 ms / 2,000 ms
コード長 2,259 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 884 ms
コンパイル使用メモリ 100,980 KB
実行使用メモリ 19,592 KB
最終ジャッジ日時 2026-01-27 00:53:01
合計ジャッジ時間 4,498 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 14
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <iostream>
#include <cstring>
#include <vector>
#include <queue>

using namespace std;

typedef pair<int, int> PII;
typedef long long LL;

const int N = 2010, M = 2 * N;
const LL INF = 9e18;

LL cnt, ans, cnt_a;
bool st[N];
priority_queue<PII, vector<PII>, greater<PII>> pq;
int n, m, l, a[N], h[N], e[M], ne[M], w[M], idx, d[N][N];

void Add(int x, int y, int z) {
   e[idx] = y, w[idx] = z, ne[idx] = h[x], h[x] = idx++;
}

void Dijkstra(int s) {
    memset(st, 0, sizeof(st));
    pq.push({ 0, s });
    while (!pq.empty()) {
        auto [dist, pos] = pq.top();
        pq.pop();

        if (st[pos]) continue;
        st[pos] = true;
        for (int i = h[pos]; i != -1; i = ne[i]) {
            int j = e[i];
            if (d[s][j] > dist + w[i]) {
                d[s][j] = dist + w[i];
                pq.push({ d[s][j], j });
            }
        }
    }
}

int main() {
    // freopen("truck.in", "r", stdin);
    // freopen("truck.out", "w", stdout);
    // 全在一起记得打0

    scanf("%d%d%d", &n, &m, &l);
    for (int i = 1; i <= n; ++i) {
        scanf("%d", &a[i]);
        if (a[i] > 0) ++cnt_a;
    }
    memset(h, -1, sizeof(h));
    for (int i = 1, x, y, z; i <= m; ++i) {
        scanf("%d%d%d", &x, &y, &z);
        Add(x, y, z), Add(y, x, z);
    }

    if (cnt_a == 1) {
        puts("0");
        return 0;
    }
    memset(d, 0x3f, sizeof(d));
    for (int i = 1; i <= n; ++i) d[i][i] = 0;
    for (int i = 1; i <= n; ++i) {
        Dijkstra(i);
    }

    ans = INF;
    for (int i = 1; i <= n; ++i) { // 遍历每种集合点
        cnt = 0LL;
        if (i != l && a[l] == 0) {
            for (int j = 1; j <= n; ++j) {
                if (j == i) continue;
                cnt += 2LL * a[j] * d[i][j];
            }
            LL mi = INF;
            for (int j = 1; j <= n; ++j) {
                if (!a[j]) continue;
                mi = min(mi, cnt - d[i][j] + d[l][j]);
            }
            ans = min(ans, mi);
        } else {
            --a[l];
            cnt += d[i][l];
            for (int j = 1; j <= n; ++j) {
                cnt += 2LL * a[j] * d[i][j];
            }
            ++a[l];
            ans = min(ans, cnt);
        }
    }

    printf("%lld\n", ans);
    
    return 0;
}
0