95 pts, WA on #51 求调
查看原帖
95 pts, WA on #51 求调
999244
Aurie楼主2025/6/30 18:26

提交记录:https://www.luogu.com.cn/record/221875362

代码:

#include <bits/stdc++.h>
#define int long long
using namespace std;

const int N = 15, INF = 0x3f3f3f3f;

int n, m, a[N][N], dp[N][1 << N], cost[1 << 12][1 << 12];
void dmin(int& x, const int& y) {
    if (y < x) x = y;
}

void init() {
    for (int to = 1; to < (1 << n); to++) {
        for (int fr = to; fr; fr = (fr - 1) & to) {
            if (fr == to) continue;
            for (int i = 0; i < n; i++) {
                if (!(((fr ^ to) >> i) & 1)) continue;
                int tmp = INF;
                for (int j = 0; j < n; j++) {
                    if ((fr >> j) & 1) dmin(tmp, a[j + 1][i + 1]);
                }
                if (tmp >= INF) {cost[fr][to] = INF; break;}
                else cost[fr][to] += tmp;
            }
        }
    }
//    for (int i = 0; i < (1 << n); i++) {
//    	for (int j = i; j; j = (j - 1) & i) {
//    		if (i == j) continue;
//			cout << cost[j][i] << endl; 
//		}
//	}
}

int calc(int id) {
    memset(dp, 0x3f, sizeof dp);
    dp[0][1 << id] = 0;
    int res = INF;
    for (int i = 1; i < n; i++) {
        for (int A = 1; A < (1 << n); A++) {
            for (int S = A; S; S = (S - 1) & A) {
                if (S == A) continue;
                dmin(dp[i][A], dp[i - 1][S] + i * cost[S][A]);
            }
            if (A == (1 << n) - 1) dmin(res, dp[i][A]);
        }
    }
    return res;
}

signed main() {
    memset(a, 0x3f, sizeof a);
    cin >> n >> m;
    int x, y, v;
    for (int i = 1; i <= m; i++) {
        cin >> x >> y >> v;
        dmin(a[x][y], v);
        dmin(a[y][x], v);
    }
    init();
    int ans = INF;
    for (int i = 0; i < n; i++) dmin(ans, calc(i));
    cout << ans << endl;
    return 0;
}
2025/6/30 18:26
加载中...