Submission #310327

#TimeUsernameProblemLanguageResultExecution timeMemory
310327BeanZCities (BOI16_cities)C++14
14 / 100
2513 ms44868 KiB
// I_Love_LPL
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define endl '\n'
const int N = 1e5 + 5;
const int mod = 1e9 + 7;
ll dp[(1 << 5) + 1][N], p[N];
vector<pair<ll, ll>> node[N];
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    if (fopen("A.inp", "r")){
            freopen("test.inp", "r", stdin);
            freopen("test.out", "w", stdout);
    }
    ll n, k, m;
    cin >> n >> k >> m;
    for (int i = 0; i < (1 << k); i++){
        for (int j = 1; j <= n; j++){
            dp[i][j] = 1e18;
        }
    }
    for (int i = 0; i < k; i++) cin >> p[i], dp[(1 << i)][p[i]] = 0;
    for (int i = 1; i <= m; i++){
        ll u, v, c;
        cin >> u >> v >> c;
        node[u].push_back({v, c});
        node[v].push_back({u, c});
    }
    for (int i = 1; i < (1 << k); i++){
        ll now = -1;
        for (int j = 0; j < k; j++){
            if (!(i & (1 << j))) continue;
            now = j;
            break;
        }
        priority_queue<pair<ll, ll>, vector<pair<ll, ll>>, greater<pair<ll, ll>>> pq;
        for (int j = 1; j <= n; j++){
            if (now != -1) dp[i][j] = min(dp[i][j], dp[i ^ (1 << now)][j] + dp[(1 << now)][j]);
            pq.push({dp[i][j], j});
        }
        while (pq.size()){
            pair<ll, ll> x = pq.top();
            pq.pop();
            if (x.first != dp[i][x.second]) continue;
            for (auto j : node[x.second]){
                if ((x.first + j.second) < dp[i][j.first]){
                    dp[i][j.first] = x.first + j.second;
                    pq.push({dp[i][j.first], j.first});
                }
            }
        }
    }
    ll ans = 1e18;
    for (int i = 1; i <= n; i++){
        ans = min(ans, dp[(1 << k) - 1][i]);
    }
    cout << ans;
}
/*
*/

Compilation message (stderr)

cities.cpp: In function 'int main()':
cities.cpp:15:20: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   15 |             freopen("test.inp", "r", stdin);
      |             ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
cities.cpp:16:20: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
   16 |             freopen("test.out", "w", stdout);
      |             ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...