Submission #1073307

# Submission time Handle Problem Language Result Execution time Memory
1073307 2024-08-24T12:08:55 Z Jarif_Rahman Cities (BOI16_cities) C++17
100 / 100
1365 ms 47916 KB
#include <bits/stdc++.h>
#define pb push_back
#define f first
#define sc second
using namespace std;
typedef long long int ll;
typedef string str;

const ll inf = 1e18;
const int N = 1e5;

int n, m, k;
vector<pair<int, ll>> graph[N];
bitset<N> bl;

void dijkstra(ll* dis){
    bl.reset();
    priority_queue<pair<ll, int>> pq;
    for(int s = 0; s < n; s++) if(dis[s] < inf) pq.push({-dis[s], s});
    
    while(!pq.empty()){
        int nd = pq.top().second;
        pq.pop();
        if(bl[nd]) continue;
        bl[nd] = 1;
        for(auto [x, w]: graph[nd]) if(dis[nd]+w < dis[x]){
            dis[x] = dis[nd]+w;
            pq.push({-dis[x], x});
        }
    }
}

int ic[5];
ll dis[32][N];

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin >> n >> k >> m;

    for(int i = 0; i < k; i++) cin >> ic[i], ic[i]--;

    for(int i = 0; i < m; i++){
        int a, b; ll w; cin >> a >> b >> w; a--, b--;
        graph[a].push_back({b, w});
        graph[b].push_back({a, w});
    }

    for(int i = 0; i < k; i++){
        fill(dis[1<<i], dis[1<<i]+n, inf);
        dis[1<<i][ic[i]] = 0;
        dijkstra(dis[1<<i]);
    }

    for(int mask = 1; mask < (1<<k); mask++){
        if(__builtin_popcount(mask) < 2) continue;
        fill(dis[mask], dis[mask]+n, inf);
        for(int s = 0; s < k; s++) if(mask&(1<<s)) {
            for(int i = 0; i < n; i++)
                dis[mask][i] = min(dis[mask][i], dis[1<<s][i]+dis[mask^(1<<s)][i]);
        }
        dijkstra(dis[mask]);
    }

    cout << *min_element(dis[(1<<k)-1], dis[(1<<k)-1]+n) << "\n";
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 2908 KB Output is correct
2 Correct 1 ms 2652 KB Output is correct
3 Correct 1 ms 2652 KB Output is correct
4 Correct 1 ms 2908 KB Output is correct
5 Correct 1 ms 3164 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 314 ms 29096 KB Output is correct
2 Correct 331 ms 28440 KB Output is correct
3 Correct 172 ms 19284 KB Output is correct
4 Correct 43 ms 15296 KB Output is correct
5 Correct 198 ms 23908 KB Output is correct
6 Correct 71 ms 15488 KB Output is correct
7 Correct 3 ms 3164 KB Output is correct
8 Correct 2 ms 2908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 5 ms 3164 KB Output is correct
2 Correct 6 ms 3356 KB Output is correct
3 Correct 3 ms 3164 KB Output is correct
4 Correct 4 ms 2908 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 712 ms 35220 KB Output is correct
2 Correct 613 ms 34740 KB Output is correct
3 Correct 401 ms 25568 KB Output is correct
4 Correct 404 ms 26896 KB Output is correct
5 Correct 117 ms 17548 KB Output is correct
6 Correct 47 ms 17748 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1365 ms 47916 KB Output is correct
2 Correct 1323 ms 47788 KB Output is correct
3 Correct 1329 ms 47344 KB Output is correct
4 Correct 868 ms 37988 KB Output is correct
5 Correct 743 ms 33292 KB Output is correct
6 Correct 174 ms 19024 KB Output is correct
7 Correct 65 ms 18256 KB Output is correct