結果

問題 No.1812 Uribo Road
ユーザー MasKoaTSMasKoaTS
提出日時 2021-12-29 21:20:40
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 1,807 bytes
コンパイル時間 1,084 ms
コンパイル使用メモリ 99,096 KB
実行使用メモリ 13,888 KB
最終ジャッジ日時 2024-04-15 03:15:27
合計ジャッジ時間 7,858 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
13,764 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 30 ms
6,940 KB
testcase_04 AC 2 ms
6,944 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 2 ms
6,944 KB
testcase_08 AC 40 ms
6,940 KB
testcase_09 AC 95 ms
6,940 KB
testcase_10 AC 25 ms
6,940 KB
testcase_11 AC 14 ms
6,940 KB
testcase_12 TLE -
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 #

#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] == inf) {
						pq.push({ d + nd,nv,l });
					}
				}
				else {
					int nl = l | (1 << g);
					if (dist[nv][nl] == inf) {
						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