Run ID 作者 问题 语言 测评结果 分数 时间 内存 代码长度 提交时间
51542 AK2022071336 最优子图 C++ 运行超时 0 2000 MS 13988 KB 1842 2022-07-13 11:51:49

Tests(0/20):


#include <bits/stdc++.h> using namespace std; const int N = 1005; int n, k, in, h[N], chose[N], ans, vis[N], w[N][N], cnt; bool tong[N]; struct ab { int v, w, nxt; }mp[N * N]; void add(int x, int y, int z) { mp[++cnt].v = y; mp[cnt].w = z; mp[cnt].nxt = h[x]; h[x] = cnt; } void dfs(int u, int fa, int val, int cnt) { if(tong[u > n ? u - n : u + n]) return; if(cnt == n) { int tmp = 0; for(int i = 1; i <= n; i++) { if(chose[i] > n) tmp++; else tmp--; } if(tmp == n or tmp == -n) return; for(int i = h[u]; i; i = mp[i].nxt) { int v = mp[i].v; if(v == fa) continue; if(tong[v]) { val += mp[i].w; } } // printf("%d ", val); ans = max(ans, val); // for(int i = 1; i <= n; i++) // { // printf("%d ", chose[i]); // } // printf("\n"); } for(int i = h[u]; i; i = mp[i].nxt) { int v = mp[i].v; if(v == fa) continue; if(tong[v]) { val += mp[i].w; } } // if(vis[u] > val) return; // vis[u] = val; for(int i = h[u]; i; i = mp[i].nxt) { int v = mp[i].v; if(tong[v]) continue; tong[v] = 1; chose[cnt + 1] = v; dfs(v, u, val + mp[i].w, cnt + 1); tong[v] = 0; } } int main () { scanf("%d%d", &n, &k); if(k == 1) { printf("%d\n", n - 1); return 0; } for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) { scanf("%d", &in); w[i][j] = in; } } for(int i = 1; i <= n << 1; i++) { for(int j = 1; j <= n << 1; j++) { if(i == j) continue; if(i <= n and j <= n) add(i, j, w[i][j]); if((i > n and j <= n) or (i <= n and j > n)) add(i, j, k - w[i > n ? i - n : i][j > n ? j - n : j]); if(i > n and j > n) add(i, j, w[i - n][j - n]); } } add(0, 1, 0); add(0, 1 + n, 0); dfs(0, 0, 0, 0); printf("%d", ans); return 0; }


测评信息: