# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
59947 | IvanC | Cities (BOI16_cities) | C++17 | 3284 ms | 83672 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> leve;
typedef pair<ll,int> ii;
typedef tuple<ll,int,int> trinca;
const int MAXN = 1e5 + 10;
const int MAXK = 7;
const int MAXL = 35;
const ll INF = 1e18;
vector<leve> grafo[MAXN];
ll matriz[MAXN][MAXK],menor_distancia[MAXN][MAXL];
int N,K,M,qual[MAXK],processado[MAXN];
int jafoi[MAXN][MAXL];
void Dijkstra(int idx){
memset(processado,0,sizeof(processado));
priority_queue<ii, vector<ii> , greater<ii> > pq;
pq.push(ii(0,qual[idx]));
while(!pq.empty()){
ii davez = pq.top();
pq.pop();
int v = davez.second;
ll path = davez.first;
if(processado[v]) continue;
processado[v] = 1;
matriz[v][idx] = path;
for(ii aresta : grafo[v]){
int u = aresta.first;
ll w = aresta.second;
if(!processado[u]) pq.push(ii(path + w,u));
}
}
}
int main(){
scanf("%d %d %d",&N,&K,&M);
for(int i = 0;i<K;i++){
scanf("%d",&qual[i]);
}
for(int i = 1;i<=M;i++){
int u,v,w;
scanf("%d %d %d",&u,&v,&w);
grafo[u].push_back(leve(v,w));
grafo[v].push_back(leve(u,w));
}
for(int i = 0;i<K;i++) Dijkstra(i);
ll resposta = INF;
int alvo = (1 << K) - 1;
for(int i = 1;i<=N;i++){
for(int bitmask = 0;bitmask <= alvo;bitmask++){
menor_distancia[i][bitmask] = INF;
}
}
for(int i = 1;i<=N;i++){
for(int j = 0;j<K;j++){
menor_distancia[i][(1 << j)] = matriz[i][j];
}
}
for(int bitmask = 1;bitmask < alvo;bitmask++){
if(!(bitmask & (1 << 0))) continue;
priority_queue<ii,vector<ii>,greater<ii>> pq;
for(int i = 1;i<=N;i++){
if(menor_distancia[i][bitmask] != INF) pq.push(ii(menor_distancia[i][bitmask],i));
}
while(!pq.empty()){
ii davez = pq.top();
pq.pop();
ll path = davez.first;
int v = davez.second;
if(jafoi[v][bitmask]) continue;
jafoi[v][bitmask] = 1;
for(leve aresta : grafo[v]){
int u = aresta.first, w = aresta.second;
if(path + w < menor_distancia[u][bitmask]){
menor_distancia[u][bitmask] = path + w;
pq.push(ii(path + w,u));
}
}
}
for(int j = 0;j<K;j++){
if(bitmask & (1 << j)) continue;
int nbitmask = (bitmask) | (1 << j);
for(int i = 1;i<=N;i++){
menor_distancia[i][nbitmask] = min(menor_distancia[i][nbitmask], menor_distancia[i][bitmask] + matriz[i][j] );
}
}
}
for(int i = 1;i<=N;i++) resposta = min(resposta,menor_distancia[i][alvo]);
printf("%lld\n",resposta);
return 0;
}
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... |