結果

問題 No.1812 Uribo Road
ユーザー MasKoaTSMasKoaTS
提出日時 2021-11-14 22:10:15
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,926 bytes
コンパイル時間 1,089 ms
コンパイル使用メモリ 93,864 KB
実行使用メモリ 7,520 KB
最終ジャッジ日時 2023-10-01 02:28:37
合計ジャッジ時間 7,640 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 8 ms
4,376 KB
testcase_03 TLE -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

// bitDP なし

#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;
	}

	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);

	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