結果

問題 No.1812 Uribo Road
ユーザー MasKoaTS
提出日時 2021-12-29 21:31:17
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 1,811 bytes
コンパイル時間 1,377 ms
コンパイル使用メモリ 95,284 KB
最終ジャッジ日時 2025-01-27 07:33:32
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 20 TLE * 9 MLE * 1
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cassert>
#include <vector>
#include <queue>
#include <set>
#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 get_rnum(int K, VV<int>&edge, V<int>&R, int s, int t) {
	rep(i, 0, K) {
		if (s == edge[R[i]][0] and t == edge[R[i]][1]) {
			return i;
		}
		else if (s == edge[R[i]][1] and t == edge[R[i]][0]) {
			return i;
		}
	}
	return -1;
}


int dijkstra(int N, int K, VV<int>&edge, V<int>&R, VV<Pair>&route) {
	VV<int> dist(N, V<int>(1 << K, inf));
	priority_queue<V<int>, V<V<int> >, greater<V<int> > > pq;
	pq.push({ 0,0,0 });
	while (pq.size()) {
		V<int> p = pq.top();	pq.pop();
		int d = p[0];	int v = p[1];	int l = p[2];
		if (v == N - 1 and l == (1 << K) - 1) {
			return d;
		}
		if (dist[v][l] == inf) {
			dist[v][l] = d;
			for (auto& r : route[v]) {
				int nv = r.first;	int nd = r.second;
				int g = get_rnum(K, edge, R, v, nv);
				if (g == -1) {
					if (dist[nv][l] > d + nd) {
						pq.push({ d + nd,nv,l });
					}
				}
				else {
					int nl = l | (1 << g);
					if (dist[nv][nl] > d + nd) {
						pq.push({ d + nd,nv,nl });
					}
				}
			}
		}
	}
	assert(false);
}


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 = dijkstra(N, K, edge, R, route);

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