結果

問題 No.1814 Uribo Road (Easy)
ユーザー MasKoaTS
提出日時 2022-01-12 22:12:50
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 38 ms / 2,000 ms
コード長 2,161 bytes
コンパイル時間 1,149 ms
コンパイル使用メモリ 91,368 KB
最終ジャッジ日時 2025-01-27 10:38:44
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 44
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <queue>
#define rep(i, l, n) for (int i = (l); i < (n); i++)
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>(0));
	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];
		V<int> di(n, -1);
		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 (di[v] == -1) {
				cnt++;
				di[v] = d;
				rep(j, 0, route[v].size()) {
					Pair p = { d + route[v][j].second,route[v][j].first };
					pq.push(p);
				}
			}
		}
		dist[k] = di;
	}
	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] });
	}
	VV<int> dist = dijkstra(N, edge, R, route);


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

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

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