結果

問題 No.1814 Uribo Road (Easy)
ユーザー MasKoaTSMasKoaTS
提出日時 2021-10-16 23:53:42
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 30 ms / 2,000 ms
コード長 1,419 bytes
コンパイル時間 1,123 ms
コンパイル使用メモリ 85,968 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-05-01 17:26:41
合計ジャッジ時間 3,027 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 3 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 3 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 4 ms
5,376 KB
testcase_12 AC 6 ms
5,376 KB
testcase_13 AC 5 ms
5,376 KB
testcase_14 AC 7 ms
5,376 KB
testcase_15 AC 9 ms
5,376 KB
testcase_16 AC 13 ms
5,376 KB
testcase_17 AC 14 ms
5,376 KB
testcase_18 AC 14 ms
5,376 KB
testcase_19 AC 14 ms
5,376 KB
testcase_20 AC 20 ms
5,376 KB
testcase_21 AC 17 ms
5,376 KB
testcase_22 AC 25 ms
5,376 KB
testcase_23 AC 23 ms
5,376 KB
testcase_24 AC 23 ms
5,376 KB
testcase_25 AC 22 ms
5,376 KB
testcase_26 AC 26 ms
5,376 KB
testcase_27 AC 29 ms
5,376 KB
testcase_28 AC 29 ms
5,376 KB
testcase_29 AC 30 ms
5,376 KB
testcase_30 AC 29 ms
5,376 KB
testcase_31 AC 30 ms
5,376 KB
testcase_32 AC 5 ms
5,376 KB
testcase_33 AC 2 ms
5,376 KB
testcase_34 AC 2 ms
5,376 KB
testcase_35 AC 7 ms
5,376 KB
testcase_36 AC 4 ms
5,376 KB
testcase_37 AC 5 ms
5,376 KB
testcase_38 AC 9 ms
5,376 KB
testcase_39 AC 4 ms
5,376 KB
testcase_40 AC 5 ms
5,376 KB
testcase_41 AC 25 ms
5,376 KB
testcase_42 AC 14 ms
5,376 KB
testcase_43 AC 5 ms
5,376 KB
testcase_44 AC 9 ms
5,376 KB
testcase_45 AC 3 ms
5,376 KB
testcase_46 AC 24 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <algorithm>
#define rep(i, l, n) for (int i = (l); i < (n); i++)
#define all(x) x.begin(), x.end()
using namespace std;
template <class T>
using V = vector<T>;
template <class T>
using VV = V<V<T> >;
const int inf = 1000000000;


void warshall_floyd(int N, VV<int>&dist) {
	rep(i, 0, N) {
		dist[i][i] = 0;
	}
	rep(k, 0, N) {
		rep(i, 0, N) {
			rep(j, 0, N) {
				int d = dist[i][k] + dist[k][j];
				if (dist[i][j] > d) {
					dist[i][j] = d;
				}	
			}
		}
	}			
}


int main(void) {
	int N, M, K;	cin >> N >> M >> K;

	V<int> R(K);
	rep(i, 0, K) {
		cin >> R[i];	R[i] -= 1;
	}
	sort(all(R));

	VV<int> edge(M, V<int>(3));
	VV<int> dist(N, V<int>(N, inf));
	rep(i, 0, M) {
		rep(j, 0, 3) {
			cin >> edge[i][j];
		}
		edge[i][0] -= 1;	edge[i][1] -= 1;
		int a = edge[i][0];	int b = edge[i][1];
		dist[a][b] = dist[b][a] = edge[i][2];
	}
	warshall_floyd(N, dist);

	int ans = inf;
	int rsum = 0;
	rep(i, 0, R.size()) {
		rsum += edge[R[i]][2];
	}

	do {
		rep(bit, 0, 1 << K) {
			int s = rsum;
			V<int> path(1);
			rep(i, 0, K) {
				int b = (bit >> i) & 1;
				path.push_back(edge[R[i]][b]);
				path.push_back(edge[R[i]][b ^ 1]);
			}
			path.push_back(N - 1);
			for (int i = 0; i < path.size() - 1; i += 2) {
				s += dist[path[i]][path[i + 1]];
			}
			if (ans > s) {
				ans = s;
			}
		}
	} while (next_permutation(all(R)));

	cout << ans << endl;
	return 0;
}
0