# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
230385 | arnold518 | Cities (BOI16_cities) | C++14 | 6069 ms | 171856 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;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 1e5;
const ll INF = 1e18;
int N, M, K;
int A[10];
vector<pii> adj[MAXN+10];
ll dist[MAXN+10][40];
struct Queue
{
int v, mask; ll w;
bool operator < (const Queue &p) const { return w>p.w; }
};
int main()
{
int i, j;
scanf("%d%d%d", &N, &K, &M);
for(i=0; i<K; i++) scanf("%d", &A[i]);
for(i=1; i<=M; i++)
{
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
adj[u].push_back({v, w});
adj[v].push_back({u, w});
}
for(i=1; i<=N; i++) for(j=1; j<(1<<K); j++) dist[i][j]=INF;
priority_queue<Queue> PQ;
for(i=0; i<K; i++)
{
PQ.push({A[i], (1<<i), 0});
dist[A[i]][(1<<i)]=0;
}
while(!PQ.empty())
{
Queue now=PQ.top(); PQ.pop();
if(now.w>dist[now.v][now.mask]) continue;
//printf("%d %d %lld\n", now.v, now.mask, now.w);
for(auto &nxt : adj[now.v])
{
int p=(((1<<K)-1)^now.mask);
for(i=p; ; i=p&(i-1))
{
if(dist[nxt.first][i|now.mask]>now.w+dist[nxt.first][i]+nxt.second)
{
dist[nxt.first][i|now.mask]=now.w+dist[nxt.first][i]+nxt.second;
PQ.push({nxt.first, i|now.mask, dist[nxt.first][i|now.mask]});
}
if(i==0) break;
}
}
}
ll ans=INF;
for(i=1; i<=N; i++) ans=min(ans, dist[i][(1<<K)-1]);
printf("%lld\n", ans);
}
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... |