結果

問題 No.788 トラックの移動
ユーザー veqccveqcc
提出日時 2019-02-14 17:29:42
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 445 ms / 2,000 ms
コード長 2,048 bytes
コンパイル時間 920 ms
コンパイル使用メモリ 97,572 KB
実行使用メモリ 34,896 KB
最終ジャッジ日時 2023-08-21 17:30:43
合計ジャッジ時間 4,089 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 412 ms
34,836 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,384 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 97 ms
19,276 KB
testcase_05 AC 402 ms
34,856 KB
testcase_06 AC 412 ms
34,836 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 1 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 1 ms
4,388 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 108 ms
34,816 KB
testcase_16 AC 445 ms
34,896 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <set>
#include <map>
typedef long long ll;
typedef unsigned int uint;
using namespace std;

int N, M, L;
ll inf = 1LL << 60;
typedef pair<int, ll> P;
ll D[2005][2005]; // cost[i][j] -> iからjへの最小コスト
ll truck[2005];
vector<P> G[2005]; // edge[a] is vector of P(b,c) ( witch means [a->b with cost c] )

void dijkstra(int from) {
    priority_queue <P, vector<P>, greater<P>> q;
    for (int i = 0; i < N; i++) {
        D[from][i] = inf;
    }
    D[from][from] = 0;
    q.push(P(0, from));
    while (!q.empty()) {
        P p = q.top();
        q.pop();
        ll y = p.second; // p.second means current node position
        if (D[from][y] != p.first) continue;
        for (int i = 0; i < G[y].size(); i++) {
            int node = G[y].at(i).first; // edge y->node
            ll cost = G[y].at(i).second; // cost of y->node
            if (D[from][node] > D[from][y] + cost) {
                D[from][node] = D[from][y] + cost;
                q.push(P(D[from][node], node)); // P(total cost, current node)
            }
        }
    }
}

int main() {
    cin.sync_with_stdio(false);
    cin.tie(0);
    cin >> N >> M >> L;
    L--;

    for (int i = 0; i < N; i++) {
        cin >> truck[i];
    }

    for (int i = 0; i < M; i++) {
        int a, b;
        ll c;
        cin >> a >> b >> c;
        a--; b--;
        G[a].push_back(P(b,c));
        G[b].push_back(P(a,c));
    }

    for (int i = 0; i < N; i++) {
        dijkstra(i);
    }

    ll mn = inf;
    for (int i = 0; i < N; i++) {
        ll sm = 0;
        for (int j = 0; j < N; j++) {
            sm += 2 * D[i][j] * truck[j];
        }

        ll mx = 0;
        for (int j = 0; j < N; j++) {
            if (truck[j] == 0) continue;

            mx = max(mx, D[i][j] - D[L][j]);
        }

        sm -= mx;
        mn = min(mn, sm);
    }

    cout << mn << "\n";
    return 0;
}
0