# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
30929 | Navick | Cities (BOI16_cities) | C++14 | 4000 ms | 36228 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>
#define F first
#define S second
#define pii pair<int, int>
#define pb push_back
using namespace std;
typedef long long ll;
typedef long double ld;
const int N = 1e5 + 10, K = 5;
const ll INF = 1e18;
ll dp[N][1 << K];
vector<pii> adj[N];
int imp[K];
bool see[N];
queue<int> q;
int main(){
int n, k, m; scanf("%d %d %d", &n, &k, &m);
for(int i=0; i<k; i++){
int v; scanf("%d", &v);
imp[i] = v;
}
for(int i=0; i<m; i++){
int u, v, w; scanf("%d %d %d", &u, &v, &w);
adj[u].pb({v, w});
adj[v].pb({u, w});
}
for(int mask=1; mask<(1 << k); mask++){
while(!q.empty())
q.pop();
for(int i=1; i<=n; i++){
dp[i][mask] = INF;
for(int t=mask; t; t=mask & (t - 1))
dp[i][mask] = min(dp[i][mask], dp[i][t] + dp[i][mask ^ t]);
q.push(i);
see[i] = true;
}
if(__builtin_popcount(mask) == 1){
for(int j=0; j<k; j++)
if(mask & (1 << j))
dp[imp[j]][mask] = 0;
}
while(!q.empty()){
int v = q.front(); q.pop();
see[v] = false;
for(auto X : adj[v]){
int u = X.F, w = X.S;
if(dp[u][mask] > dp[v][mask] + w){
dp[u][mask] = dp[v][mask] + w;
if(!see[u])
q.push(u);
}
}
}
}
ll res = INF;
for(int i=1; i<=n; i++)
res = min(res, dp[i][(1 << k) - 1]);
printf("%lld\n", res);
}
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... |