結果

問題 No.1812 Uribo Road
ユーザー MasKoaTSMasKoaTS
提出日時 2021-12-31 12:47:28
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,125 ms / 5,000 ms
コード長 2,425 bytes
コンパイル時間 1,260 ms
コンパイル使用メモリ 98,468 KB
実行使用メモリ 399,592 KB
最終ジャッジ日時 2024-04-16 23:47:16
合計ジャッジ時間 10,401 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 14 ms
6,272 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 8 ms
5,376 KB
testcase_09 AC 11 ms
5,376 KB
testcase_10 AC 6 ms
5,376 KB
testcase_11 AC 5 ms
5,376 KB
testcase_12 AC 660 ms
398,336 KB
testcase_13 AC 375 ms
398,336 KB
testcase_14 AC 365 ms
398,336 KB
testcase_15 AC 136 ms
47,768 KB
testcase_16 AC 411 ms
290,432 KB
testcase_17 AC 728 ms
332,928 KB
testcase_18 AC 171 ms
8,572 KB
testcase_19 AC 1,020 ms
399,232 KB
testcase_20 AC 1,125 ms
399,232 KB
testcase_21 AC 894 ms
399,588 KB
testcase_22 AC 983 ms
399,592 KB
testcase_23 AC 10 ms
5,376 KB
testcase_24 AC 2 ms
5,376 KB
testcase_25 AC 72 ms
76,288 KB
testcase_26 AC 6 ms
5,376 KB
testcase_27 AC 159 ms
6,428 KB
testcase_28 AC 314 ms
374,016 KB
testcase_29 AC 2 ms
5,376 KB
testcase_30 AC 18 ms
5,376 KB
testcase_31 AC 416 ms
69,560 KB
testcase_32 AC 4 ms
5,376 KB
testcase_33 AC 129 ms
105,088 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;


int dijkstra(int n, int start, int finish, VV<Pair>& route) {
	V<int> path(n, -1);
	priority_queue<Pair, V<Pair>, greater<Pair> > pq;
	pq.push({ 0,start });
	while (pq.size()) {
		Pair p = pq.top();	pq.pop();
		int d = p.first;	int v = p.second;
		if (path[v] == -1) {
			path[v] = d;
			if (v == finish) {
				break;
			}
			rep(i, 0, route[v].size()) {
				Pair p = { d + route[v][i].second,route[v][i].first };
				pq.push(p);
			}
		}
	}
	return path[finish];
}


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(N, V<int>(N, inf));


	V<VV<int> > dp(1 << K, VV<int>(K, V<int>(2, inf)));
	rep(i, 0, K) {
		rep(j, 0, 2) {
			int p = 0;	int q = edge[R[i]][j];
			if (dist[p][q] == inf) {
				dist[p][q] = dist[q][p] = dijkstra(N, p, q, route);
			}
			dp[1 << i][i][j] = rsum + dist[p][q];
		}
	}
	rep(bit, 1, 1 << K) {
		rep(s, 0, K) {
			if ((bit & (1 << s)) == 0) {
				continue;
			}
			rep(i, 0, 2) {
				rep(t, 0, K) {
					if (bit & (1 << t)) {
						continue;
					}
					int b = bit | (1 << t);
					rep(j, 0, 2) {
						int p = edge[R[s]][i];	int q = edge[R[t]][j];
						if (dist[p][q] == inf) {
							dist[p][q] = dist[q][p] = dijkstra(N, p, q, route);
						}
						int d = dp[bit][s][i ^ 1] + dist[p][q];
						if (dp[b][t][j] > d) {
							dp[b][t][j] = d;
						}
					}
				}
			}
		}
	}

	ans = inf;
	rep(i, 0, K) {
		rep(j, 0, 2) {
			int p = edge[R[i]][j];	int q = N - 1;
			if (dist[p][q] == inf) {
				dist[p][q] = dist[q][p] = dijkstra(N, p, q, route);
			}
			int d = dp[(1 << K) - 1][i][j ^ 1] + dist[p][q];
			if (ans > d) {
				ans = d;
			}
		}
	}

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