이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
// Success consists of going from failure to failure without loss of enthusiasm
#include <bits/stdc++.h>
using namespace std;
#define nl '\n'
#define f first
#define s second
#define mp make_pair
#define pb push_back
#define sz(x) int(x.size())
template<class T> using V = vector<T>;
template<class T> using pq = priority_queue<T, vector<T>, greater<T>>;
using ll = long long;
using vl = V<ll>;
using vi = V<int>;
using pl = pair<ll, ll>;
using vpl = V<pl>;
const ll INFL = 2e18;
int main() {
cin.tie(0)->sync_with_stdio(0);
int N, K, M; cin >> N >> K >> M;
V<vl> dp(N, vl(1 << K, INFL));
for(int i = 0; i < K; i++) {
int x; cin >> x; --x;
dp[x][1 << i] = 0;
}
V<vpl> adj(N); for(int i = 0; i < M; i++) {
int u, v, w; cin >> u >> v >> w; --u, --v;
adj[u].pb(mp(v, w));
adj[v].pb(mp(u, w));
}
pq<pl> q;
for(int m = 1; m < (1 << K); m++) {
for(int t = 0; t < 2; t++) {
for(int u = 0; u < N; u++) q.push(mp(dp[u][m], u));
vi vis(N);
while(sz(q)) {
int u; ll d; tie(d, u) = q.top(); q.pop();
// cout << u << " " << d << endl;
if (vis[u]) continue;
vis[u] = 1;
for(auto& e : adj[u]) {
int v, w; tie(v, w) = e;
if (dp[v][m] > (dp[u][m] + w)) {
dp[v][m] = dp[u][m] + w;
q.push(mp(dp[v][m], v));
}
}
}
// cout << m << endl;
for(int u = 0; u < N; u++) {
for(int sub = m; sub > 0; sub = (sub - 1) & m) {
// cout << u << " => " << sub << " " << (m ^ sub);
// cout << " => " << dp[u][sub] << " " << dp[u][m ^ sub] << endl;
dp[u][m] = min(dp[u][m], dp[u][sub] + dp[u][m ^ sub]);
}
// cout << u << " " << dp[u][m] << endl;
}
// cout << endl;
}
}
ll ans = INFL;
for(int u = 0; u < N; u++) ans = min(ans, dp[u][(1 << K) - 1]);
cout << ans << nl;
exit(0-0);
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |