제출 #1325519

#제출 시각아이디문제언어결과실행 시간메모리
1325519pastaCities (BOI16_cities)C++20
100 / 100
5173 ms44264 KiB
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define pb push_back
#define S  second
#define F  first
#define all(x)		(x).begin(),(x).end()
//#define int			long long
//#define lc			(id * 2)
//#define rc			(lc + 1)
//#define mid			((l + r) / 2)
const int maxn = 2e5 + 10;
const int mod = 998244353;
const ll inf = 1e18 + 10;

ll n, k, m, dp[(1 << 6)][maxn], vt[maxn];
vector<pll> G[maxn];


signed main() {
	cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0);
	cin >> n >> k >> m;
	
	for (int i = 1; i <= k; i++) {
		cin >> vt[i];
	}
	
	for (int i = 1; i <= m; i++) {
		ll v, u, c;
		cin >> v >> u >> c;
		G[v].pb({u, c});
		G[u].pb({v, c});
	}
	
	for (int i = 0; i < (1ll << k); i++) {
		for (int j = 1; j <= n; j++) {
			dp[i][j] = inf;
		}
	}
	for (int i = 1; i <= n; i++) 
		dp[0][i] = 0ll;
	for (int i = 1; i <= k; i++)
		dp[(1ll << (i - 1))][vt[i]] = 0ll;
		
	for (int mask = 0; mask < (1ll << k); mask++) {
		for (int sub = mask; ; sub = (sub - 1) & mask) {
			for (int u = 1; u <= n; u++) {
				dp[mask][u] = min(dp[mask][u], dp[sub][u] + dp[mask ^ sub][u]);
			}
			if (sub == 0)
				break;
		}
		set<pll> se;
		for (int i = 1; i <= n; i++)
			se.insert({dp[mask][i], i});
		while (!se.empty()) {
			auto p = *se.begin();
			se.erase(p);
			ll cos = p.F;
			int v = p.S;
			
			for (auto [u, w] : G[v]) {
				ll nw = cos + w;
				if (dp[mask][u] > nw) {
					se.erase({dp[mask][u], u});
					dp[mask][u] = nw;
					se.insert({dp[mask][u], u});
				}
			}
		}
	}
	
	ll ans = inf;
	for (int i = 1; i <= n; i++)
		ans = min(ans, dp[(1 << k) - 1][i]);
	cout << ans << '\n';
}

#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...