新人求助,宝藏那题,luogu100 uoj97
查看原帖
新人求助,宝藏那题,luogu100 uoj97
87696
Lily_White楼主2020/6/7 08:11

以下是我的代码

(我看了一下好象和某一篇题解思路类似,但我还是看不懂评论区里的hack数据)

#include <bits/stdc++.h>
using namespace std;
const int N = 13;
const int INF = 0x3f3f3f3f;
int d[N][N], out[N][N], cnt[N], dp[1 << 13], dep[N];
int n, m;
namespace debug {
	string toString(int x) {
		string ret;
		while (x) {
			ret.push_back((x & 1) + '0');
			x >>= 1;
		}
		while (ret.length() < n) ret = "0" + ret;
		return ret;
	}
}
void dfs(int s) {
	cerr << "Calling DFS(" << debug::toString(s) << ")" << endl;
    for (int i = 1; i <= n; i++)
        if (s & (1 << (i - 1)))
            for (int j = 1; j <= n; j++) 
                if (d[i][j] != INF && !(s & (1 << (j - 1)))) 
                    if (dp[1 << (j - 1) | s] > d[i][j] * dep[i] + dp[s]) {
                        int tmp = dep[j];
                        dep[j] = dep[i] + 1;
                        dp[1 << (j - 1) | s] = d[i][j] * dep[i] + dp[s];
                        cerr << "Attempting to use edge (" << i << "," << j << ")" << endl;
                        dfs(1 << (j - 1) | s);
                        dep[j] = tmp;
                    }
}
int main() {
//	freopen("treasure.in", "r", stdin);
    cin >> n >> m;
    memset(d, INF, sizeof(d));
    for (int i = 1; i <= m; i++) {
        int u, v, w;
        cin >> u >> v >> w;
        if (d[u][v] < w) continue;  //we cannot update it with w
        d[u][v] = d[v][u] = w;
    }
    int ans = INF;
    for (int i = 1; i <= n; i++) {
		cerr << "Processing i=" << i << endl;
        memset(dep, INF, sizeof(dep));
        memset(dp, INF, sizeof(dp)); 
        dp[0] = 0;
        dep[i] = 1;		//set i to be this time's root
        dp[1 << (i - 1)] = 0;
        dfs(1 << (i - 1));
        ans = min(ans, dp[(1 << n) - 1]);
    } 
    cout << ans << endl;
}

2020/6/7 08:11
加载中...