#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);
set<trinca> pq;
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 = 0;i<K;i++){
pq.insert(make_tuple(0,(1 << i),qual[i]));
}
while(!pq.empty()){
trinca davez = *(pq.begin());
pq.erase(pq.begin());
ll path = get<0>(davez);
int bitmask = get<1>(davez), v = get<2>(davez);
if(jafoi[v][bitmask]) continue;
jafoi[v][bitmask] = 1;
if(bitmask == alvo){
printf("%lld\n",path);
return 0;
}
for(int i = 0;i<K;i++){
if(bitmask & (1 << i)) continue;
int mascara = bitmask | (1 << i);
ll w = matriz[v][i];
if(path + w < menor_distancia[v][mascara]){
if(menor_distancia[v][mascara] != INF) pq.erase(make_tuple(menor_distancia[v][mascara],mascara, v ));
menor_distancia[v][mascara] = path + w;
pq.insert(make_tuple(path + matriz[v][i],mascara, v ));
}
}
for(leve aresta : grafo[v]){
int u = aresta.first;
int w = aresta.second;
if(path + w < menor_distancia[u][bitmask]){
if(menor_distancia[u][bitmask] != INF) pq.erase(make_tuple(menor_distancia[u][bitmask],bitmask, v ));
menor_distancia[u][bitmask] = path + w;
pq.insert(make_tuple(path + w,bitmask,u));
}
}
}
return 0;
}
Compilation message
cities.cpp: In function 'int main()':
cities.cpp:40:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d",&N,&K,&M);
~~~~~^~~~~~~~~~~~~~~~~~~~~
cities.cpp:42:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&qual[i]);
~~~~~^~~~~~~~~~~~~~~
cities.cpp:46:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d",&u,&v,&w);
~~~~~^~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
3064 KB |
Output is correct |
2 |
Correct |
4 ms |
3172 KB |
Output is correct |
3 |
Correct |
5 ms |
3376 KB |
Output is correct |
4 |
Correct |
4 ms |
3376 KB |
Output is correct |
5 |
Correct |
5 ms |
3376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2567 ms |
84492 KB |
Output is correct |
2 |
Correct |
2992 ms |
87800 KB |
Output is correct |
3 |
Correct |
451 ms |
87800 KB |
Output is correct |
4 |
Correct |
311 ms |
87800 KB |
Output is correct |
5 |
Correct |
933 ms |
87800 KB |
Output is correct |
6 |
Correct |
234 ms |
87800 KB |
Output is correct |
7 |
Correct |
12 ms |
87800 KB |
Output is correct |
8 |
Correct |
10 ms |
87800 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
87800 KB |
Output is correct |
2 |
Correct |
25 ms |
87800 KB |
Output is correct |
3 |
Correct |
17 ms |
87800 KB |
Output is correct |
4 |
Correct |
16 ms |
87800 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
6019 ms |
121356 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
6017 ms |
146820 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |