# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
361276 | Killer2501 | Cities (BOI16_cities) | C++14 | 5325 ms | 139404 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define task "talltree"
#define pll pair<ll, ll>
#define pii pair<ll, pll>
#define fi first
#define se second
#define ull unsigned long long
using namespace std;
const ll mod = 1e15+7;
const ll N = 2e5+5;
vector<pll> adj[N];
vector<ll> kq;
ll n, m, t, k, b[N], p, lab[N], u, v, h[N], a[N], ans, tong, d[N][32];
priority_queue< pii, vector<pii>, greater<pii> > pq;
void sol()
{
cin >> n >> k >> m;
for(int i = 0; i < k; i ++)cin >> a[i];
for(int i = 1; i <= m; i ++)
{
ll x, y, w;
cin >> x >> y >> w;
adj[x].pb({w, y});
adj[y].pb({w, x});
}
for(int i = 1; i <= n; i ++)
{
pii u;
u.fi = 0;
u.se.fi = i;
u.se.se = 0;
pq.push(u);
for(int j = 1; j < (1<<k);j ++)d[i][j] = mod;
}
for(int i = 0; i < k; i ++)
{
d[a[i]][(1<<i)] = 0;
pii u;
u.fi = 0;
u.se.fi = a[i];
u.se.se = (1<<i);
pq.push(u);
}
while(!pq.empty())
{
pii u = pq.top();
pq.pop();
if(d[u.se.fi][u.se.se] != u.fi)continue;
if(u.se.se == (1<<k)-1)
{
cout << u.fi;
return;
}
for(int j = 0; j < k; j ++)
{
t = u.se.se | (1<<j);
if(d[u.se.fi][t] > u.fi + d[u.se.fi][(1<<j)])
{
d[u.se.fi][t] = u.fi + d[u.se.fi][(1<<j)];
pii v;
v.fi = d[u.se.fi][t];
v.se.fi = u.se.fi;
v.se.se = t;
pq.push(v);
}
}
for(pll x : adj[u.se.fi])
{
if(d[x.se][u.se.se] > u.fi + x.fi)
{
d[x.se][u.se.se] = u.fi + x.fi;
pii v;
v.fi = u.fi + x.fi;
v.se.fi = x.se;
v.se.se = u.se.se;
pq.push(v);
}
}
}
}
int main()
{
if(fopen(task".inp", "r")){
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int ntest = 1;
//cin >> ntest;
while(ntest -- > 0)
sol();
}
Compilation message (stderr)
# | 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... |