結果

問題 No.1812 Uribo Road
ユーザー kwm_tkwm_t
提出日時 2022-01-15 05:47:15
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 109 ms / 5,000 ms
コード長 2,632 bytes
コンパイル時間 2,212 ms
コンパイル使用メモリ 185,632 KB
実行使用メモリ 13,836 KB
最終ジャッジ日時 2024-05-01 01:30:41
合計ジャッジ時間 4,183 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
8,136 KB
testcase_01 AC 5 ms
8,140 KB
testcase_02 AC 4 ms
8,192 KB
testcase_03 AC 15 ms
10,712 KB
testcase_04 AC 5 ms
7,992 KB
testcase_05 AC 4 ms
8,260 KB
testcase_06 AC 4 ms
8,156 KB
testcase_07 AC 6 ms
8,360 KB
testcase_08 AC 6 ms
8,236 KB
testcase_09 AC 7 ms
8,380 KB
testcase_10 AC 5 ms
8,404 KB
testcase_11 AC 6 ms
8,204 KB
testcase_12 AC 49 ms
13,220 KB
testcase_13 AC 25 ms
13,232 KB
testcase_14 AC 27 ms
13,260 KB
testcase_15 AC 33 ms
9,568 KB
testcase_16 AC 32 ms
10,448 KB
testcase_17 AC 89 ms
11,892 KB
testcase_18 AC 24 ms
11,916 KB
testcase_19 AC 107 ms
13,704 KB
testcase_20 AC 109 ms
13,836 KB
testcase_21 AC 84 ms
13,824 KB
testcase_22 AC 68 ms
13,812 KB
testcase_23 AC 7 ms
8,560 KB
testcase_24 AC 4 ms
8,016 KB
testcase_25 AC 13 ms
9,228 KB
testcase_26 AC 5 ms
8,144 KB
testcase_27 AC 16 ms
10,068 KB
testcase_28 AC 24 ms
9,584 KB
testcase_29 AC 5 ms
7,996 KB
testcase_30 AC 7 ms
8,352 KB
testcase_31 AC 56 ms
12,260 KB
testcase_32 AC 5 ms
8,420 KB
testcase_33 AC 38 ms
12,236 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
//#include <atcoder/all>
using namespace std;
//using namespace atcoder;
//using mint = modint1000000007;
//const int mod = 1000000007;
//using mint = modint998244353;
//const int mod = 998244353;
//const int INF = 1e9;
const long long LINF = 1e18;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i,l,r)for(int i=(l);i<(r);++i)
#define rrep(i, n) for (int i = (n-1); i >= 0; --i)
#define rrep2(i,l,r)for(int i=(r-1);i>=(l);--i)
#define all(x) (x).begin(),(x).end()
#define allR(x) (x).rbegin(),(x).rend()
#define endl "\n"
#define P pair<long long,int>
template<typename A, typename B> inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; }
template<typename A, typename B> inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; }
struct Edge {
	int to;
	long long cost;
	Edge(int _to, long long _cost) :to(_to), cost(_cost) {}
};
vector<Edge>g[200005];
int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int n, m, l; cin >> n >> m >> l;
	vector<int>r(l);
	rep(i, l)cin >> r[i], r[i]--;
	vector<int>a(m), b(m), c(m);
	rep(i, m) {
		cin >> a[i] >> b[i] >> c[i];
		a[i]--; b[i]--;
		g[a[i]].emplace_back(b[i], c[i]);
		g[b[i]].emplace_back(a[i], c[i]);
	}
	vector<vector<long long>> dist(l * 2, vector<long long>(n, LINF));
	rep(i, l * 2) {// dikstra
		int st = 0;
		if (0 == i % 2)st = a[r[i / 2]];
		else st = b[r[i / 2]];
		priority_queue<P, vector<P>, greater<P>> q;
		q.push({ 0, st });
		dist[i][st] = 0;
		while (!q.empty()) {
			auto tmp = q.top();
			q.pop();
			long long cost = tmp.first;
			int pos = tmp.second;
			if (cost > dist[i][pos]) continue;
			for (Edge e : g[pos]) {
				long long ncost = cost + e.cost;
				int npos = e.to;
				if (chmin(dist[i][npos], ncost)) q.push({ ncost, npos });
			}
		}
	}
	vector<vector<vector<long long>>> dp(1 << l, vector<vector<long long>>(l, vector<long long>(2, LINF)));
	rep(i, l) {
		dp[1 << i][i][0] = dist[2 * i + 1][0] + c[r[i]];
		dp[1 << i][i][1] = dist[2 * i + 0][0] + c[r[i]];
	}
	rep(i, 1 << l) rep(j, l)rep(k, 2) {
		if (LINF == dp[i][j][k])continue;
		rep(nj, l) {
			if (1 & (i >> nj))continue;
			int ni = i | (1 << nj);
			rep(nk, 2) {
				int st, ed;
				if (0 == k)st = j * 2;
				else st = j * 2 + 1;
				if (0 == nk)ed = b[r[nj]];
				else ed = a[r[nj]];
				long long ncost = dp[i][j][k] + dist[st][ed] + c[r[nj]];
				chmin(dp[ni][nj][nk], ncost);
			}
		}
	}
	long long ans = LINF;
	rep(i, l) rep(j, 2) {
		long long sub = dp[(1 << l) - 1][i][j];
		sub += dist[i * 2 + j][n - 1];
		chmin(ans, sub);
	}
	cout << ans << endl;
	return 0;
}
0