# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
27468 | top34051 | Cities (BOI16_cities) | C++14 | 4000 ms | 50696 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 maxn 100005
#define inf (long long)1e16
struct node {
int x,bit;
long long val;
node(int _x = 0,int _bit = 0,long long _val = 0) {
x = _x; bit = _bit; val = _val;
}
bool operator < (node a) const {
return a.val<val;
}
};
int n,k,m;
int p2[10];
int sp[maxn];
long long mem[maxn][35];
vector<pair<int,int> > from[maxn];
priority_queue<node> heap;
main() {
int i,x,y,val,bit;
node t;
scanf("%d%d%d",&n,&k,&m);
for(i=1;i<=n;i++) sp[i] = -1;
for(i=0;i<k;i++) scanf("%d",&x), sp[x] = i;
while(m--) {
scanf("%d%d%d",&x,&y,&val);
from[x].push_back({y,val}); from[y].push_back({x,val});
}
p2[-1] = 0; p2[0] = 1;
for(i=1;i<=k;i++) p2[i] = p2[i-1]*2;
for(x=1;x<=n;x++) {
for(bit=0;bit<p2[k];bit++) mem[x][bit] = inf;
if(sp[x]!=-1) {
mem[x][p2[sp[x]]] = 0;
heap.push(node(x,p2[sp[x]],0));
}
}
while(!heap.empty()) {
t = heap.top(); heap.pop();
if(t.bit==p2[k]-1) {
printf("%lld",t.val);
return 0;
}
// for(bit=0;bit<p2[k];bit++) {
// if(mem[t.x][t.bit|bit] > mem[t.x][t.bit] + mem[t.x][bit]) {
// mem[t.x][t.bit|bit] = mem[t.x][t.bit] + mem[t.x][bit];
// heap.push(node(t.x,t.bit|bit,mem[t.x][t.bit|bit]));
// }
// }
for(i=0;i<from[t.x].size();i++) {
y = from[t.x][i].first; val = from[t.x][i].second;
if(mem[y][t.bit|p2[sp[y]]] > mem[t.x][t.bit] + val) {
mem[y][t.bit|p2[sp[y]]] = mem[t.x][t.bit] + val;
heap.push(node(y,t.bit|p2[sp[y]],mem[y][t.bit|p2[sp[y]]]));
}
}
}
}
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... |