답안 #1039239

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1039239 2024-07-30T15:19:03 Z Whisper Cities (BOI16_cities) C++17
100 / 100
2588 ms 49584 KB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

#define int long long
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FORD(i, a, b) for (int i = (b); i >= (a); i --)
#define REP(i, a) for (int i = 0; i < (a); ++i)
#define REPD(i, a) for (int i = (a) - 1; i >= 0; --i)

#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)


constexpr ll LINF = (1ll << 60);
constexpr int INF = (1ll << 30);
constexpr int Mod = 1e9 + 7;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

/*
    Phu Trong from Nguyen Tat Thanh High School for gifted student
*/

template <class X, class Y>
    bool minimize(X &x, const Y &y){
        X eps = 1e-9;
        if (x > y + eps) {x = y; return 1;}
        return 0;
    }

template <class X, class Y>
    bool maximize(X &x, const Y &y){
        X eps = 1e-9;
        if (x + eps < y) {x = y; return 1;}
        return 0;
    }
#define MAX_NODE             100005
#define MAX_EDGE             200005
int numNode, numCity, numEdge;

struct Edge{
    int u, v, w;
    Edge(){}
    Edge(int _u, int _v, int _w): u(_u), v(_v), w(_w){}

    int other(int x){return (x ^ u ^ v);}
} edge[MAX_EDGE];

vector<int> G[MAX_NODE];

bool good[MAX_NODE];

int dp[MAX_NODE][MASK(5)];

int id[MAX_NODE];
int cnt = 0;

void upd(int x){
    priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q;
    for (int i = 1; i <= numNode; ++i){
        q.emplace(dp[i][x], i);
    }
    while(q.size()){
        int du, u; tie(du, u) = q.top(); q.pop();
        if (du > dp[u][x]) continue;
        for (int&i : G[u]){
            int v = edge[i].other(u);
            if(minimize(dp[v][x], dp[u][x] + edge[i].w)){
                q.emplace(dp[v][x], v);
            }
        }
    }
}
void process(void){
    cin >> numNode >> numCity >> numEdge;
    for (int i = 1; i <= numCity; ++i){
        int u; cin >> u;
        good[u] = true;
        id[u] = cnt++;
    }
    for (int i = 1; i <= numEdge; ++i){
        cin >> edge[i].u >> edge[i].v >> edge[i].w;
        G[edge[i].u].emplace_back(i);
        G[edge[i].v].emplace_back(i);
    }
    memset(dp, 0x3f, sizeof dp);
    for (int i = 1; i <= numNode; ++i) {
        if (good[i]){
            dp[i][MASK(id[i])] = 0;
        }
    }

    for (int msk = 0; msk < MASK(numCity); ++msk){
        for (int sub_msk = msk; sub_msk > 0; sub_msk = (msk & (sub_msk - 1))){
            for (int i = 1; i <= numNode; ++i){
                minimize(dp[i][msk], dp[i][msk ^ sub_msk] + dp[i][sub_msk]);
            }
        }
        upd(msk);
    }

    int ans = LINF;
    for (int i = 1; i <= numNode; ++i) minimize(ans, dp[i][MASK(numCity) - 1]);
    cout << ans;
}
signed main(){
    #define name "Whisper"
    cin.tie(nullptr) -> sync_with_stdio(false);
    //freopen(name".inp", "r", stdin);
    //freopen(name".out", "w", stdout);
    process();
    return (0 ^ 0);
}





# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 27740 KB Output is correct
2 Correct 11 ms 27740 KB Output is correct
3 Correct 11 ms 27844 KB Output is correct
4 Correct 10 ms 27736 KB Output is correct
5 Correct 11 ms 27736 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 650 ms 49472 KB Output is correct
2 Correct 657 ms 49516 KB Output is correct
3 Correct 376 ms 39092 KB Output is correct
4 Correct 62 ms 40692 KB Output is correct
5 Correct 299 ms 49504 KB Output is correct
6 Correct 55 ms 40592 KB Output is correct
7 Correct 13 ms 27992 KB Output is correct
8 Correct 13 ms 27996 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 15 ms 27992 KB Output is correct
2 Correct 15 ms 27996 KB Output is correct
3 Correct 13 ms 27932 KB Output is correct
4 Correct 12 ms 27996 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1334 ms 49548 KB Output is correct
2 Correct 1206 ms 49452 KB Output is correct
3 Correct 800 ms 39128 KB Output is correct
4 Correct 592 ms 46148 KB Output is correct
5 Correct 159 ms 42780 KB Output is correct
6 Correct 72 ms 42364 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 2487 ms 49584 KB Output is correct
2 Correct 2588 ms 49556 KB Output is correct
3 Correct 2276 ms 49464 KB Output is correct
4 Correct 1515 ms 39116 KB Output is correct
5 Correct 1036 ms 46120 KB Output is correct
6 Correct 259 ms 43080 KB Output is correct
7 Correct 99 ms 42360 KB Output is correct