#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> ii;
typedef tuple<ll,int,int> trinca;
const int MAXN = 2*1e5 + 10;
const int MAXK = 7;
const int MAXL = 35;
const ll INF = 1e15;
vector<ii> grafo[MAXN];
ll matriz[MAXN][MAXK];
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;
pq.push(ii(path + w,u));
}
}
}
int main(){
cin.tie(0);ios_base::sync_with_stdio(0);
cin >> N >> K >> M;
for(int i = 0;i<K;i++){
cin >> qual[i];
}
for(int i = 1;i<=M;i++){
int u,v,w;
cin >> u >> v >> w;
grafo[u].push_back(ii(v,w));
grafo[v].push_back(ii(u,w));
}
for(int i = 0;i<K;i++) Dijkstra(i);
priority_queue<trinca,vector<trinca>, greater<trinca> > pq;
for(int i = 0;i<K;i++){
pq.push(make_tuple(0,(1 << i),qual[i]));
}
int alvo = (1 << K) - 1;
while(!pq.empty()){
trinca davez = pq.top();
pq.pop();
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){
cout << path << endl;
return 0;
}
for(int i = 0;i<K;i++){
if(bitmask & (1 << i)) continue;
int mascara = bitmask | (1 << i);
if(!jafoi[v][mascara]) pq.push(make_tuple(path + matriz[v][i],mascara, v ));
}
for(ii aresta : grafo[v]){
int u = aresta.first;
ll w = aresta.second;
if(!jafoi[u][bitmask]) pq.push(make_tuple(path + w,bitmask,u));
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
5752 KB |
Output is correct |
2 |
Correct |
7 ms |
5860 KB |
Output is correct |
3 |
Correct |
6 ms |
5936 KB |
Output is correct |
4 |
Correct |
7 ms |
6012 KB |
Output is correct |
5 |
Correct |
7 ms |
6112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1512 ms |
52340 KB |
Output is correct |
2 |
Correct |
1227 ms |
52340 KB |
Output is correct |
3 |
Correct |
298 ms |
52340 KB |
Output is correct |
4 |
Correct |
527 ms |
52340 KB |
Output is correct |
5 |
Correct |
682 ms |
52340 KB |
Output is correct |
6 |
Correct |
299 ms |
52340 KB |
Output is correct |
7 |
Correct |
13 ms |
52340 KB |
Output is correct |
8 |
Correct |
10 ms |
52340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
52340 KB |
Output is correct |
2 |
Correct |
17 ms |
52340 KB |
Output is correct |
3 |
Correct |
12 ms |
52340 KB |
Output is correct |
4 |
Correct |
15 ms |
52340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3528 ms |
101600 KB |
Output is correct |
2 |
Correct |
2904 ms |
101600 KB |
Output is correct |
3 |
Correct |
1672 ms |
101600 KB |
Output is correct |
4 |
Correct |
2921 ms |
101600 KB |
Output is correct |
5 |
Correct |
876 ms |
101600 KB |
Output is correct |
6 |
Correct |
818 ms |
101600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
6040 ms |
167076 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |