# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
410716 | antontsiorvas | Cities (BOI16_cities) | C++14 | 2655 ms | 42224 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 <cstdio>
#include <vector>
#include <utility>
#include <queue>
#include <algorithm>
#define lli long long int
using namespace std;
int N, K, M, import[7], from, to;
lli weight, dp[33][100005];
vector< pair<int, lli> > data[100005];
priority_queue< pair<lli, int> > pq;
lli inf = 9000000000000000000;
int main(){
scanf("%d%d%d",&N,&K,&M);
for(int i=1; i<=K; i++) scanf("%d",&import[i]);
for(int i=1; i<=M; i++){
scanf("%d%d%lld",&from,&to,&weight);
data[from].push_back(make_pair(to,weight));
data[to].push_back(make_pair(from,weight));
}
int to = (1 << K);
for(int i=0; i<to; i++){
for(int j=1; j<=N; j++) dp[i][j] = inf;
}
for(int i=0; i<K; i++) dp[1 << i][import[i+1]] = 0;
for(int i=1; i<to; i++){
for(int prev=0; prev<i; prev++){
int b_or = (prev | i), b_xor = (prev ^ i);
if(b_or != i || prev < b_xor) continue;
for(int j=1; j<=N; j++){
dp[i][j] = min(dp[i][j],dp[prev][j]+dp[b_xor][j]);
}
}
for(int j=1; j<=N; j++){
if(dp[i][j] != inf) pq.push(make_pair(-dp[i][j],j));
}
while(!pq.empty()){
int city = pq.top().second;
lli dpnum = -pq.top().first;
pq.pop();
int len = data[city].size();
for(int j=0; j<len; j++){
int next_city = data[city][j].first;
lli cost = data[city][j].second;
if(dpnum + cost < dp[i][next_city]){
dp[i][next_city] = dpnum + cost;
pq.push(make_pair(-dp[i][next_city],next_city));
}
}
}
}
lli ans = inf;
for(int i=1; i<=N; i++) ans = min(ans, dp[to-1][i]);
printf("%lld",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... |