1 条题解

  • 0
    @ 2025-10-16 19:29:16

    loj记录里的存货,代码经过Loj格式化。

    #include <bits/stdc++.h>
    using namespace std;
    #define getchar() (bufS==bufT&&(bufT=(bufS=bufB)+fread(bufB,1,1<<15,stdin),bufS==bufT)?EOF:*bufS++)
    char bufB[1 << 15], *bufS = bufB, *bufT = bufB;
    namespace get_out {
    char op;
    inline void read_(int &x) {
        x = 0;
        int fi(1);
        op = getchar();
    
        while ((op < '0' || op > '9') && op != '-')
            op = getchar();
    
        if (op == '-')
            op = getchar(), fi = -1;
    
        while (op >= '0' && op <= '9')
            x = (x << 1) + (x << 3) + (op ^ 48), op = getchar();
    
        x *= fi;
    }
    template<typename Head, typename ...Tail>
    inline void read_(Head &h, Tail &... t) {
        read_(h), read_(t...);
    }
    
    inline void postive_write(int x) {
        if (x > 9)
            postive_write(x / 10);
    
        putchar(x % 10 | '0');
    }
    inline void write_(int x) {
        if (x < 0)
            x = -x, putchar('-');
    
        postive_write(x);
    }
    }
    using get_out::read_;
    using get_out::write_;
    #define chkmin(a,b) a=a<b?a:b
    int n, m, k, dp[1025][105];
    bool vis[105];
    vector<pair<int, int>> e[105];
    inline void spfa(int S) {
        static queue<int> q;
    
        if (!q.empty())
            q = queue<int>();
    
        for (int i = 0; i < n; ++i)
            if (dp[S][i] != 0x3f3f3f3f)
                q.emplace(i),
                          vis[i] = 1;
    
        int u;
    
        while (!q.empty()) {
            u = q.front();
            q.pop();
            vis[u] = 0;
    
            for (auto [v, w] : e[u])
                if (dp[S][u] + w < dp[S][v]) {
                    dp[S][v] = dp[S][u] + w;
    
                    if (!vis[v])
                        q.emplace(v), vis[v] = 1;
                }
        }
    }
    int main() {
        read_(n, m, k);
    
        for (int i = 1, u, v, w; i <= m; ++i)
            read_(u, v, w), e[u - 1].emplace_back(v - 1, w), e[v - 1].emplace_back(u - 1, w);
    
        memset(dp, 0x3f, sizeof dp);
    
        for (int i = 0, x; i < k; ++i)
            read_(x), dp[1 << i][x - 1] = 0;
    
        for (int S = 0; S < (1 << k); spfa(S++))
            for (int T = S & (S - 1); T; T = (T - 1)&S) {
                if (T < (S ^ T))
                    break;
    
                for (int i = 0; i < n; ++i)
                    dp[S][i] = dp[S][i] < dp[T][i] + dp[T ^ S][i] ? dp[S][i] : dp[T][i] + dp[T ^ S][i];
            }
    
        int as = 0x3f3f3f3f;
    
        for (int i = 0, p = (1 << k) - 1; i < n; ++i)
            as = as < dp[p][i] ? as : dp[p][i];
    
        write_(as);
    }
    
    • 1

    信息

    ID
    15203
    时间
    1000ms
    内存
    512MiB
    难度
    10
    标签
    递交数
    1
    已通过
    1
    上传者