CF1310D

题目链接

如果不考虑限制的话,可以直接设 \(f_{i,j}\) 表示当前在第 \(i\) 个城市中,已经走了 \(j\) 个城市了,那么 \(f_{i,j}=\min\{f_{k,j-1}+w\}\) 时间复杂度 \(O(n^2k)\)

如果再加上不能有奇环呢,可以直接对每一个点进行随机化( \(0/1\) ),然后每个点可通行,只能是不相同的,这样就可以处理掉了。

那么至于要随机多少次,首先假设已经知道了最终答案选的 \(k\) 个点,那么随中的黑白概率为 \(\frac{1}{2^{k-1}}\) 最小情况下为 \(\frac{1}{512}\) 如果计算 \(3000\) 次随不中的概率为 \((\frac{511}{512})^{3000}≈0.0028367265710469523564714721504\) 基本可以忽略不计(如果保险的话可以更大一些)。

\(\mathscr{Code:}\)

#include<bits/stdc++.h>
#define LL long long
//#define int LL
#define per(i, a, b) for (int i = a, END##i = b; i >= END##i; i--)
#define rep(i, a, b) for (int i = a, END##i = b; i <= END##i; i++)
#define repn(x) rep(x, 1, n)
#define repm(x) rep(x, 1, m)
#define pb push_back
#define e(x) for(int i = h[x], v = to[i]; i; i = nxt[i], v = to[i])
#define E(x) for(auto y : p[x])
#define PII pair<int, int>
#define i64 unsigned long long
#define YY puts("Yes"), exit(0)
#define NN puts("No"), exit(0)
using namespace std;
const int Mod = 1e9 + 7;
const int Inf = 0x3f3f3f3f;
const LL InfLL = 0x3f3f3f3f3f3f3f3f;
inline LL read() {LL s = 0, fu = 1; char ch = getchar(); while (ch < '0' || ch > '9') ch == '-' ? fu = -1 : 0, ch = getchar(); while (ch >= '0' && ch <= '9') s = (s << 1) + (s << 3) + (ch ^ 48), ch = getchar(); return s * fu;}

const int N = 110, M = 20;
LL n, k, f[N][M], g[N][N], ans = 1e18, c[N];

inline void Main() {
    srand(time(0));
    n = read(), k = read();
    repn(i) repn(j)
        g[i][j] = read();
    rep(T, 1, 3000) {
        memset(f, 0x3f, sizeof(f));
        f[1][0] = 0;
        repn(i) c[i] = rand() & 1;
        rep(j, 1, k) repn(i) rep(p, 1, n) if (c[i] != c[p]) {
            f[i][j] = min(f[i][j], f[p][j - 1] + g[p][i]);
        }
        ans = min(ans, f[1][k]);
    }
    printf("%lld\n", ans);
}

signed main() {
    // freopen("input.in", "r", stdin);
    int T = 1;
    while (T--)
        Main();
    return 0;
}
posted @ 2025-08-18 14:01  wh2011  阅读(6)  评论(0)    收藏  举报