結果

問題 No.1690 Power Grid
ユーザー snrnsidysnrnsidy
提出日時 2021-10-25 10:52:57
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 131 ms / 3,000 ms
コード長 1,401 bytes
コンパイル時間 2,047 ms
コンパイル使用メモリ 197,416 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-14 04:50:15
合計ジャッジ時間 5,448 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

long long int n,m,k,a,b,c;
long long int dp[1<<18];
long long int dist[18][18];
long long int A[18];

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

	cin >> n >> m >> k;

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

	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			if (i == j)
			{
				dist[i][j] = 0;
			}
			else
			{
				dist[i][j] = 1e18;
			}
		}
	}


	for(int i=0;i<m;i++)
	{
		cin >> a >> b >> c;
		a-=1;
		b-=1;
		dist[a][b] = min(dist[a][b],c);
		dist[b][a] = min(dist[b][a],c);
	}

	for(int k=0;k<n;k++)
	{
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<n;j++)
			{
				if(dist[i][j] > dist[i][k] + dist[k][j])
				{
					dist[i][j] = dist[i][k] + dist[k][j];
				}
			}
		}
	}

	for(int i=0;i<(1<<n);i++)
	{
		dp[i] = 1e18;
	}

	dp[0] = 0;

	for(int i=0;i<(1<<n);i++)
	{
		for(int j=0;j<n;j++)
		{
			if((i&(1<<j))) continue;
			long long int val = 1e18;
			for(int k=0;k<n;k++)
			{
				if((i&(1<<k)))
				{
					val = min(val,dist[j][k]);
				}
			}
			if(val>=1e18) val = 0;
			//cout << j << ' ' << val << ' ' << A[j] << '\n';
			dp[i | (1<<j)] = min(dp[i | (1<<j)],dp[i] + val + A[j]);
		}
	}

	long long int res = 1e18;

	for(int i=0;i<(1<<n);i++)
	{
		int cnt = 0;
		for(int j=0;j<n;j++)
		{
			if((i&(1<<j))) cnt++;
		}
		if(cnt==k)
		{
			res = min(res,dp[i]);
		}
	}

	cout << res << '\n';
	return 0;
}
0