結果

問題 No.3312 Fire Engine
コンテスト
ユーザー sclara
提出日時 2025-10-24 12:16:58
言語 C++23
(gcc 15.2.0 + boost 1.90.0)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 43 ms / 2,000 ms
+ 398µs
コード長 1,210 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,053 ms
コンパイル使用メモリ 337,868 KB
実行使用メモリ 14,208 KB
最終ジャッジ日時 2026-07-15 21:21:35
合計ジャッジ時間 4,807 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 42
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

template<typename T, typename U>
bool chmax(T& a, U b) {
    if (a < b) {
        a = b;
        return true;
    } else {
        return false;
    }
}

template<typename T, typename U>
bool chmin(T& a, U b) {
    if (a > b) {
        a = b;
        return true;
    } else {
        return false;
    }
}

int main()
{
    int n;
    cin >> n;
    vector<vector<ll>> d(n, vector<ll>(n));
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            cin >> d[i][j];
        }
    }
    vector<vector<ll>> dist((1 << n), vector<ll>(n, 1e18));
    dist[1][0] = 0;
    dist[0][0] = 0;
    for (int i = 1; i < (1 << n); ++i) {
        bitset<16> bi(i);
        ll count = 0;
        for (int j = 0; j < n; ++j) {
            if (bi[j] == 0) count++;
        }
        for (int j = 0; j < n; ++j) {
            for (int k = 0; k < n; ++k) {
                if (bi[k] == 1) continue;
                chmin(dist[(i | (1 << k))][k], dist[i][j] + d[j][k] * count);
            }
        }
    }
    ll ans = 1e18;
    for (int i = 0; i < n; ++i) {
        chmin(ans, dist[(1 << n) - 1][i]);
    }
    cout << ans << endl;
}
0