# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
62050 | Crown | Cities (BOI16_cities) | C++14 | 262 ms | 44956 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>
using namespace std;
#define X first
#define Y second
#define pb push_back
typedef pair<int, int> ii;
typedef long long ll;
typedef vector<int> vi;
typedef vector< ii > vii;
const int maxn = 1e5+5;
const int maxk = 5;
vii adj[maxn];
int n, k, m;
int imp[maxk+5];
ll dist[(1<<5)+5][maxn];
int mark[maxn];
struct node
{
int bit, u;
ll d;
node(int _bit, int _u, ll _d)
{
bit = _bit; u = _u; d = _d;
}
bool operator < (node other) const
{
return d> other.d;
}
};
int main()
{
memset(mark, -1, sizeof mark);
scanf("%d %d %d", &n, &k, &m);
for(int i = 0; i< k; i++)
{
scanf("%d", imp+i);
mark[imp[i]] = i;
}
for(int i = 1; i<= m; i++)
{
int u, v, w; scanf("%d %d %d", &u, &v, &w);
adj[u].pb(ii(v, w));
adj[v].pb(ii(u, w));
}
memset(dist, 32, sizeof dist);
priority_queue<node> pq;
for(int i = 1; i<= n; i++)
{
dist[0][i] = 0;
pq.push(node(0, i, 0));
}
for(int i = 0; i< k; i++)
{
dist[1<<i][imp[i]] = 0;
pq.push(node(1<<i, imp[i], 0));
}
while(!pq.empty())
{
node x = pq.top(); pq.pop();
int bit = x.bit, u = x.u;
ll d = x.d;
if(d> dist[bit][u]) continue;
for(auto edge : adj[u])
{
int v = edge.X, w = edge.Y;
if(d+w < dist[bit][v])
{
dist[bit][v] = d+w;
for(int comp = 0; comp< (1<<k); comp++)
{
if(dist[bit|comp][v]> dist[bit][v]+dist[comp][v])
{
dist[bit|comp][v] = dist[bit][v]+dist[comp][v];
pq.push(node(bit|comp, v, dist[bit|comp][v]));
}
}
}
}
}
ll best = 1e18;
for(int i = 1; i<= n; i++) best = min(best, dist[(1<<k)-1][i]);
printf("%lld\n", best);
}
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... |