This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// BOI16_cities
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int mod = 1000 * 1000 * 1000 + 7;
const int INF = 1e9 + 100;
const ll LINF = 1e18 + 100;
#ifdef DEBUG
#define dbg(x) cout << #x << " = " << (x) << endl << flush;
#define dbgr(s, f) { cout << #s << ": "; for (auto _ = (s); _ != (f); _++) cout << *_ << ' '; cout << endl << flush; }
#else
#define dbg(x) ;
#define dbgr(s, f) ;
#endif
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define fast_io ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define mp make_pair
#define fr first
#define sc second
#define endl '\n'
#define MAXN 100100
#define MAXK 6
int n, k, m;
vector<int> imp;
vector<pii> adj[MAXN];
ll dp[1 << MAXK][MAXN];
int32_t main(void)
{
fast_io;
cin >> n >> k >> m;
FOR(i, 0, k)
{
int x;
cin >> x;
x--;
imp.pb(x);
}
FOR(i, 0, m)
{
int x, y, w;
cin >> x >> y >> w;
x--; y--;
adj[x].pb({y, w});
adj[y].pb({x, w});
}
FOR(i, 0, 1 << k) fill(dp[i], dp[i] + MAXN, LINF);
FOR(i, 0, k) dp[1 << i][imp[i]] = 0;
FOR(mask, 1, 1 << k)
{
for (int sub = (mask - 1) & mask; sub; sub = (sub - 1) & mask)
FOR(v, 0, n)
dp[mask][v] = min(dp[mask][v], dp[sub][v] + dp[mask ^ sub][v]);
priority_queue<pll, vector<pll>, greater<pll>> s;
FOR(i, 0, n) s.push({dp[mask][i], i});
while (!s.empty())
{
int v = s.top().sc;
s.pop();
for (pii p : adj[v])
{
int u = p.fr;
int w = p.sc;
if (dp[mask][v] + w >= dp[mask][u]) continue;
dp[mask][u] = dp[mask][v] + w;
s.push({dp[mask][u], u});
}
}
}
ll ans = LINF;
FOR(i, 0, n) ans = min(ans, dp[(1 << k) - 1][i]);
cout << ans << endl;
return 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... |