結果

問題 No.3312 Fire Engine
コンテスト
ユーザー ルビサファせだい
提出日時 2025-10-24 22:10:56
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 33 ms / 2,000 ms
コード長 1,431 bytes
コンパイル時間 2,911 ms
コンパイル使用メモリ 286,080 KB
実行使用メモリ 13,952 KB
最終ジャッジ日時 2025-10-24 22:11:04
合計ジャッジ時間 4,936 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 42
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#include <atcoder/fenwicktree.hpp>
#include <atcoder/segtree.hpp>
#include <atcoder/modint.hpp>
#include <atcoder/dsu.hpp>
#include <atcoder/lazysegtree.hpp>

using namespace atcoder;
using namespace std;
using ll = long long;
using ull = unsigned long long;
template <class T>
using max_heap = priority_queue<T>;
template <class T>
using min_heap = priority_queue<T, vector<T>, greater<>>;
ll ll_min = numeric_limits<ll>::min();
ll ll_max = numeric_limits<ll>::max();
ll ALPHABET_N = 26;
// using mint = modint998244353;
#define rep(i, n) for (ll i = (ll)0; i < (ll)n; i++)
#define rep_(i, k, n) for (ll i = (ll)k; i < (ll)n; i++)
#define all(a) a.begin(), a.end()

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	ll n;
	cin >> n;
	vector<vector<ll>> D(n, vector<ll>(n, 0));
	rep(i, n) rep(j, n)
	{
		cin >> D[i][j];
	}
	vector dp(1LL << n, vector(n, ll_max));
	dp[1][0] = 0;
	rep_(state, 1, 1LL << n)
	{
		rep(cur, n)
		{
			if (!(state & (1LL << cur)))
				continue;
			if (dp[state][cur] == ll_max)
				continue;
			ll rest = n - __builtin_popcountll(state);
			rep(nex, n)
			{
				if (state & (1LL << nex))
					continue;
				ll nstate = state | (1LL << nex);
				ll dist = D[cur][nex];
				dp[nstate][nex] = min(dp[nstate][nex], dp[state][cur] + rest * dist);
			}
		}
	}
	ll ans = ll_max;
	rep(i, n)
	{
		ans = min(ans, dp[(1LL << n) - 1][i]);
	}
	cout << ans << endl;
	return 0;
}
0