Submission #1245849

#TimeUsernameProblemLanguageResultExecution timeMemory
1245849vtrungCities (BOI16_cities)C++20
100 / 100
2288 ms68148 KiB
#include <bits/stdc++.h>
using namespace std; 
#define ll long long
#define pll pair<ll, ll>
#define pb push_back
#define f(i, a, b) for(int i = a; i <= b; i++)
#define fi first
#define se second
#define mll map<ll, ll>
#define sll set<ll>
const ll du = 1e9 + 7;
const ll ars = 1e6 + 5;
int n, m, k;
ll dp[ars][35];
vector<pll> adj[ars];
int main() {
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    #define task "test"
    if (fopen(task".inp", "r")) {
        freopen(task".inp", "r", stdin);
        freopen(task".out", "w", stdout);
    }
    cin >> n >> k >> m;
    for(int i = 1; i <= n; i++){
        for(int mask = 1; mask < (1 << k); mask++)
            dp[i][mask] = 1e18;
    }
    for(int i = 1; i <= k; i++){
        int x; cin >> x;
        dp[x][1 << (i - 1)] = 0;
    }
    for(int i = 1; i <= m; i++){
        ll u, v, w;
        cin >> u >> v >> w;
        adj[u].pb({v, w});
        adj[v].pb({u, w});
    }
    ll ans = 1e18;
    for(int mask = 1; mask < (1 << k); mask++){
        priority_queue<pll, vector<pll>, greater<pll>> pq;
        for(int i = 1; i <= n; i++){
            for(int sub = mask; sub > 0; sub = (sub - 1) & mask)
                dp[i][mask] = min(dp[i][mask], dp[i][sub] + dp[i][mask ^ sub]);
            if(dp[i][mask] != 1e18) pq.push({dp[i][mask], i});
        }
        while(pq.size()){
            auto[cur, u] = pq.top();
            pq.pop();
            if(cur > dp[u][mask]) continue;
            for(auto[v, s] : adj[u]){
                if(cur + s < dp[v][mask]){
                    dp[v][mask] = cur + s;
                    pq.push({cur + s, v});
                }
            }
        }
    }
    for(int i = 1; i <= n; i++) ans = min(ans, dp[i][(1 << k) - 1]);
    cout << ans;
}

Compilation message (stderr)

cities.cpp: In function 'int main()':
cities.cpp:20:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   20 |         freopen(task".inp", "r", stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
cities.cpp:21:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   21 |         freopen(task".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...