結果

問題 No.17 2つの地点に泊まりたい
ユーザー ふーらくたるふーらくたる
提出日時 2016-06-24 09:47:44
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,261 bytes
コンパイル時間 397 ms
コンパイル使用メモリ 53,208 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-05 03:46:15
合計ジャッジ時間 1,526 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,384 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 2 ms
4,380 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 1 ms
4,380 KB
testcase_18 AC 2 ms
4,376 KB
testcase_19 AC 2 ms
4,380 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 2 ms
4,376 KB
testcase_22 AC 2 ms
4,380 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 2 ms
4,384 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
using namespace std;

const int kINF = 1 << 25;
const int kMAX_N = 60;
const int kMAX_M = kMAX_N * (kMAX_N - 1) / 2;

int N, M;
int S[kMAX_N];
int A[kMAX_M], B[kMAX_M], C[kMAX_M];

int cost[kMAX_N][kMAX_N];

void Solve() {
    fill((int* )cost, (int* )(cost + kMAX_N), kINF);
    for (int i = 0; i < N; i++) {
        cost[i][i] = 0;
    }

    for (int i = 0; i < M; i++) {
        cost[A[i]][B[i]] = cost[B[i]][A[i]] = C[i];
    }

    for (int k = 0; k < N; k++) {
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);
            }
        }
    }

    int answer = kINF;
    for (int node1 = 1; node1 < N - 1; node1++) {
        for (int node2 = 1; node2 < N - 1; node2++) {
            if (node1 == node2) continue;
            answer = min(answer, cost[0][node1] + cost[node1][node2] + cost[node2][N - 1] + S[node1] + S[node2]);
        }
    }
    cout << answer << endl;
}

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

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

    cin >> M;
    for (int i = 0; i < M; i++) {
        cin >> A[i] >> B[i] >> C[i];
    }

    Solve();

    return 0;
}
0