結果

問題 No.1690 Power Grid
ユーザー kabipoyokabipoyo
提出日時 2022-01-13 17:43:39
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,445 ms / 3,000 ms
コード長 2,233 bytes
コンパイル時間 4,112 ms
コンパイル使用メモリ 263,116 KB
実行使用メモリ 50,312 KB
最終ジャッジ日時 2023-08-10 21:30:02
合計ジャッジ時間 29,443 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 52 ms
50,088 KB
testcase_01 AC 50 ms
50,084 KB
testcase_02 AC 52 ms
49,984 KB
testcase_03 AC 48 ms
49,948 KB
testcase_04 AC 50 ms
50,192 KB
testcase_05 AC 51 ms
49,980 KB
testcase_06 AC 1,383 ms
50,224 KB
testcase_07 AC 1,385 ms
49,984 KB
testcase_08 AC 1,396 ms
50,084 KB
testcase_09 AC 1,445 ms
50,152 KB
testcase_10 AC 1,385 ms
50,088 KB
testcase_11 AC 1,384 ms
49,952 KB
testcase_12 AC 52 ms
49,984 KB
testcase_13 AC 52 ms
50,216 KB
testcase_14 AC 144 ms
49,964 KB
testcase_15 AC 1,407 ms
50,288 KB
testcase_16 AC 1,401 ms
50,068 KB
testcase_17 AC 617 ms
50,036 KB
testcase_18 AC 293 ms
50,148 KB
testcase_19 AC 1,398 ms
50,072 KB
testcase_20 AC 1,412 ms
49,960 KB
testcase_21 AC 1,424 ms
50,268 KB
testcase_22 AC 1,429 ms
50,224 KB
testcase_23 AC 1,361 ms
49,948 KB
testcase_24 AC 1,359 ms
49,988 KB
testcase_25 AC 1,398 ms
50,136 KB
testcase_26 AC 1,370 ms
50,216 KB
testcase_27 AC 51 ms
50,312 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
typedef int64_t lint;
#define rep(i, n) for(int i=0; i<n; i++)
#define repx(i, l, n) for(int i=l; i<n; i++)
#define all(v) v.begin(), v.end()
#define show(x) cout << #x << ": " << x << endl;
#define list(x) cout << #x << ": " << x << "  ";
#define pb push_back
using vi = vector<lint>;
using vvi = vector<vector<lint>>;
template<class T> inline void vin(vector<T>& v) { rep(i, v.size()) cin >> v.at(i); }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T> inline void drop(T x) { cout << x << endl; exit(0); }
template<class T> void vout(vector<T> v) { rep(i, v.size()) { cout << v.at(i) << ' '; } cout << endl; }
constexpr lint LINF = LLONG_MAX/2;
lint node, start;
vvi dist(18, vi(18));  // 事前に準備しておく
vector<vector<lint>> dp((1<<18), vector<lint>(18, -1));
vi A(18);

lint rec(lint bit, lint v) {
	if (dp[bit][v] != -1) return dp[bit][v];
	if (bit == (1<<v)) return dp[bit][v] = start+A[v];
	lint res = LINF,  prev_bit = bit & ~(1<<v);
	for (lint u = 0; u < node; ++u) {
		if (!(prev_bit & (1<<u))) continue;
		lint d = LINF;
		rep(i, node) {
			if ((prev_bit & (1<<i)) && i != v) chmin(d, dist[i][v]);
		}
		d += rec(prev_bit, u);
		chmin(res, d+A[v]);
	}
	return dp[bit][v] = res;
}

lint bitDP(lint N, lint s) {
	node = N, start = s;  // 初期コスト
	lint res = LINF;
	rep(i, node) chmin(res, rec((1<<node)-1, i));
	return res;
}

void Floyd_Warshall(lint N, lint M) {
	lint x, y, z;
	rep(i, N) {
		rep(j, N) {
			if (i == j) dist[i][j] = 0;
			else dist[i][j] = LINF;
		}
	}
	rep(i, M) {
		cin >> x >> y >> z;
		x--, y--;
		dist[x][y] = z;
		dist[y][x] = z;
	}
	rep(k, N) {
		rep(i, N) {
			rep(j, N) {
				chmin(dist[i][j], dist[i][k]+dist[k][j]);
			}
		}
	}
}

int main() {
	lint N, M, K;
	cin >> N >> M >> K;
	rep(i, N) cin >> A[i];
	Floyd_Warshall(N, M);

	lint a=LINF, b=0, c=0, x, y, z;
	bitDP(N, 0);
	rep(i, 1<<N) {
		if (__builtin_popcount(i) == K) {
			rep(j, N) {
				if (dp[i][j] != -1) chmin(a, dp[i][j]);
			}
		}
	}
	std::cout << a << '\n';
}
0