結果

問題 No.1814 Uribo Road (Easy)
ユーザー MasKoaTSMasKoaTS
提出日時 2021-11-07 13:01:45
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 38 ms / 2,000 ms
コード長 2,289 bytes
コンパイル時間 1,489 ms
コンパイル使用メモリ 97,004 KB
実行使用メモリ 5,876 KB
最終ジャッジ日時 2024-05-01 17:32:39
合計ジャッジ時間 3,167 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 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 3 ms
5,376 KB
testcase_11 AC 3 ms
5,376 KB
testcase_12 AC 6 ms
5,376 KB
testcase_13 AC 6 ms
5,376 KB
testcase_14 AC 8 ms
5,376 KB
testcase_15 AC 3 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 3 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 3 ms
5,376 KB
testcase_20 AC 14 ms
5,376 KB
testcase_21 AC 5 ms
5,376 KB
testcase_22 AC 23 ms
5,404 KB
testcase_23 AC 16 ms
5,376 KB
testcase_24 AC 24 ms
5,376 KB
testcase_25 AC 25 ms
5,376 KB
testcase_26 AC 32 ms
5,740 KB
testcase_27 AC 38 ms
5,872 KB
testcase_28 AC 37 ms
5,876 KB
testcase_29 AC 38 ms
5,748 KB
testcase_30 AC 36 ms
5,744 KB
testcase_31 AC 34 ms
5,748 KB
testcase_32 AC 6 ms
5,376 KB
testcase_33 AC 3 ms
5,376 KB
testcase_34 AC 3 ms
5,376 KB
testcase_35 AC 6 ms
5,376 KB
testcase_36 AC 4 ms
5,376 KB
testcase_37 AC 5 ms
5,376 KB
testcase_38 AC 11 ms
5,376 KB
testcase_39 AC 5 ms
5,376 KB
testcase_40 AC 6 ms
5,376 KB
testcase_41 AC 21 ms
5,588 KB
testcase_42 AC 12 ms
5,376 KB
testcase_43 AC 6 ms
5,376 KB
testcase_44 AC 12 ms
5,376 KB
testcase_45 AC 4 ms
5,376 KB
testcase_46 AC 23 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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


VV<int> dijkstra(int n, VV<int>& edge, V<int>& R, VV<Pair>& route) {
	VV<int> dist(n, V<int>(n, -1));
	V<int> st = { 0,n - 1 };
	rep(i, 0, R.size()) {
		st.push_back(edge[R[i]][0]);
		st.push_back(edge[R[i]][1]);
	}
	rep(i, 0, st.size()) {
		int k = st[i];
		priority_queue<Pair, V<Pair>, greater<Pair> > pq;
		pq.push({ 0,k });
		int cnt = 0;
		while (pq.size() && cnt < n) {
			Pair p = pq.top();	pq.pop();
			int d = p.first;	int v = p.second;
			if (dist[k][v] == -1) {
				cnt++;
				dist[k][v] = d;
				rep(j, 0, route[v].size()) {
					Pair p = { d + route[v][j].second,route[v][j].first };
					pq.push(p);
				}
			}
		}
	}
	return dist;
}


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<Pair> route(N, V<Pair>(0));
	rep(i, 0, M) {
		rep(j, 0, 3) {
			cin >> edge[i][j];
		}
		edge[i][0] -= 1;	edge[i][1] -= 1;
		route[edge[i][0]].push_back({ edge[i][1],edge[i][2] });
		route[edge[i][1]].push_back({ edge[i][0],edge[i][2] });
	}

	int ans = inf;
	int rsum = 0;
	rep(i, 0, R.size()) {
		rsum += edge[R[i]][2];
	}
	VV<int> dist = dijkstra(N, edge, R, route);


	rep(rbit, 0, 1 << K) {
		V<int> b(K);
		rep(i, 0, K) {
			b[i] = (rbit >> i) & 1;
		}

		VV<int> dp(1 << K, V<int>(K, inf));
		rep(i, 0, K) {
			int p = edge[R[i]][b[i]];
			dp[1 << i][i] = dist[0][p] + rsum;
		}

		rep(bit, 1, 1 << K) {
			rep(s, 0, K) {
				int p = edge[R[s]][b[s] ^ 1];
				if (((bit >> s) & 1) == 0) {
					continue;
				}
				rep(t, 0, K) {
					int np = edge[R[t]][b[t]];
					int k = bit | (1 << t);
					int d = dp[bit][s] + dist[p][np];
					if (((bit >> t) & 1) == 0 && dp[k][t] > d) {
						dp[k][t] = d;
					}
				}
			}
		}

		int c = inf;
		rep(i, 0, K) {
			int p = edge[R[i]][b[i] ^ 1];
			int d = dp[(1 << K) - 1][i] + dist[p][N - 1];
			if (c > d) {
				c = d;
			}
		}

		if (ans > c) {
			ans = c;
		}
	}
		

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