Submission #647339

# Submission time Handle Problem Language Result Execution time Memory
647339 2022-10-02T08:55:12 Z fatemetmhr Cities (BOI16_cities) C++17
100 / 100
2116 ms 38148 KB
//  ~ Be Name Khoda ~  //
 
#include<bits/stdc++.h>
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
 
using namespace std;
 
typedef long long ll;
 
#define pb       push_back
#define mp       make_pair
#define all(x)   x.begin(), x.end()
#define fi       first
#define se       second
 
const int maxn  =  1e6   + 10;
const int maxn5 =  1e5   + 10;
const int maxnt =  1.2e6 + 10;
const int maxn3 =  1e3   + 10;
const int mod   =  1e9   +  7;
const ll  inf   =  1e18;
 
vector <pair<int, int>> adj[maxn5];
ll dp[(1 << 5) + 1][maxn5];
int imp[maxn5], cmpmask, n;
priority_queue <pair<ll, int>> av;
 
inline void dij(int mask){
    while(av.size())
        av.pop();
    for(int i = 0; i < n; i++)
        av.push({-dp[mask][i], i});
    while(av.size()){
        while(av.size() && dp[mask][av.top().se] != av.top().fi * -1)
            av.pop();
        if(av.empty())
            return;
        int v = av.top().se; av.pop();
        for(auto [u, w] : adj[v]) if(dp[mask][u] > dp[mask][v] + w){
            dp[mask][u] = dp[mask][v] + w;
            av.push({-dp[mask][u], u});
        }
    }
    return;
}
 
int main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
 
    int k, m; cin >> n >> k >> m;
    for(int i = 0; i < k; i++){
        cin >> imp[i];
        imp[i]--;
    }
    for(int i = 0; i < m; i++){
        int a, b, c; cin >> a >> b >> c;
        a--; b--;
        if(a == b)
            continue;
        adj[a].pb({b, c});
        adj[b].pb({a, c});
    }
 
    for(int mask = 0; mask < (1 << k); mask++) for(int i = 0; i < n; i++)
        dp[mask][i] = inf;
 
    for(int i = 0; i < n; i++)
        dp[0][i] = 0;
 
    for(int i = 0; i < k; i++){
        dp[1 << i][imp[i]] = 0;
        for(auto [u, w] : adj[imp[i]])
            dp[1 << i][u] = min(dp[1 << i][u], ll(w));
    }
 
    for(int mask = 0; mask < (1 << k); mask++){
        dij(mask);
        for(int v = 0; v < n; v++){
            int nmask = (1 << k) - 1 - mask;
            for(int sub = nmask; sub; sub = (sub - 1) & nmask){
                dp[mask^sub][v] = min(dp[mask^sub][v], dp[mask][v] + dp[sub][v]);
            }
        }
    }
 
    ll ans = inf;
    for(int i = 0; i < n; i++)
        ans = min(ans, dp[(1 << k) - 1][i]);
    cout << ans << endl;
}
 
 
/*
8 3 9
2 8 7 
2 4 3
2 6 9
3 7 2
4 6 5
5 8 1
6 8 2
1 2 5
1 3 3
4 5 2
*/
# Verdict Execution time Memory Grader output
1 Correct 2 ms 2644 KB Output is correct
2 Correct 2 ms 2644 KB Output is correct
3 Correct 2 ms 2644 KB Output is correct
4 Correct 2 ms 2644 KB Output is correct
5 Correct 2 ms 2772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 568 ms 19348 KB Output is correct
2 Correct 584 ms 18924 KB Output is correct
3 Correct 439 ms 14140 KB Output is correct
4 Correct 71 ms 7356 KB Output is correct
5 Correct 349 ms 16192 KB Output is correct
6 Correct 69 ms 7372 KB Output is correct
7 Correct 5 ms 2900 KB Output is correct
8 Correct 5 ms 2772 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 6 ms 2900 KB Output is correct
2 Correct 8 ms 2900 KB Output is correct
3 Correct 5 ms 2900 KB Output is correct
4 Correct 5 ms 2900 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1115 ms 25560 KB Output is correct
2 Correct 1008 ms 25236 KB Output is correct
3 Correct 681 ms 20528 KB Output is correct
4 Correct 560 ms 16880 KB Output is correct
5 Correct 148 ms 9804 KB Output is correct
6 Correct 69 ms 9112 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2116 ms 38140 KB Output is correct
2 Correct 2031 ms 38148 KB Output is correct
3 Correct 1907 ms 37796 KB Output is correct
4 Correct 1374 ms 32928 KB Output is correct
5 Correct 1018 ms 23164 KB Output is correct
6 Correct 227 ms 11080 KB Output is correct
7 Correct 72 ms 9244 KB Output is correct