結果

問題 No.3312 Fire Engine
コンテスト
ユーザー Cafe1942
提出日時 2025-10-24 08:16:39
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 89 ms / 2,000 ms
コード長 1,902 bytes
コンパイル時間 1,158 ms
コンパイル使用メモリ 113,156 KB
実行使用メモリ 7,720 KB
最終ジャッジ日時 2025-10-24 08:16:44
合計ジャッジ時間 4,733 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 42
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <iomanip>//小数点出力用
//cout << fixed << setprecision(10) << ans;
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <unordered_map>
using ll = long long;
using namespace std;
#define modPHash (ll)((1LL<<61)-1)
#define modP (ll)998244353
bool chkrng0idx(int pos, int sup) { return (0 <= pos && pos < sup); }
int clk4(int num) { return (num - 2) * (num % 2); }
void yn(bool tf) { cout << (tf ? "Yes\n" : "No\n"); }

int popCount(int x) {
    int res = 0;
    while (x != 0) {
        if (x & 1)res++;
        x >>= 1;
    }
    return res;
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int N;
    cin >> N;
    bool death = 0;
    int dp[1 << 16][16] = { 0 };
    int D[16][16];
    if(N >= 17)death = 1;
    for (int i = 0;i < N;i++) {
        for (int j = 0;j < N;j++) {
            cin >> D[i][j];
            if(i == j && D[i][j] != 0)death = 1;
            if(D[i][j] >= 10001)death = 1;
        }
    }
    for(int k = 0;k < N;k++){
    	for (int i = 0;i < N;i++) {
        	for (int j = 0;j < N;j++) {
            	if(D[i][k] + D[k][j] < D[i][j])death = 1;
        	}
    	}
    }
    for (int k = 0;k < (1 << N);k++) {
        for (int i = 0;i < N;i++) {
            dp[k][i] = 1e9;
        }
    }
    for (int i = 0;i < N;i++) {
        dp[1 << i][i] = 0;
    }
    for (int k = 1;k < (1 << N);k++) {
        for (int i = 0;i < N;i++) {
            if ((k >> i) & 1) {
                for (int j = 0;j < N;j++) {
                    if (i == j)continue;
                    if ((k >> j) & 1) {
                        dp[k][i] = min(dp[k][i], dp[k - (1 << i)][j] + popCount(k - (1 << i)) * D[i][j]);
                    }
                }
            }
        }
    }
    if(death){
    	cout<<-1;return 0;
    }
    cout << dp[(1 << N) - 1][0];
}
0