CodeForces

来源:互联网 发布:博客源码下载 编辑:程序博客网 时间:2024/05/17 05:59
题意:

给出一个图的邻接矩阵,要求给每个点赋值,使得点权和为k,并定义每条边权值为两端点点权的乘积,要求最大化边的权值和。

思路:

最大化边权值之和将k均分给图中的最大团中的每个点,即ans=k^2*(best-1)/(2*best)。

证明过程:Here we go


代码:

#include <bits/stdc++.h>using namespace std;const int maxn = 45;bool mp[maxn][maxn];int some[maxn][maxn], none[maxn][maxn], all[maxn][maxn];int n, k, ans;void dfs(int d, int an, int sn, int nn){if(!sn && !nn) ans = max(ans, an);int u = some[d][0];for(int i = 0; i < sn; ++i){int v = some[d][i];if(mp[u][v]) continue;for(int j = 0; j < an; ++j)all[d+1][j] = all[d][j];all[d+1][an] = v;int tsn = 0, tnn = 0;for(int j = 0; j < sn; ++j)if(mp[v][some[d][j]])some[d+1][tsn++] = some[d][j];for(int j = 0; j < nn; ++j)if(mp[v][none[d][j]])none[d+1][tnn++] = none[d][j];dfs(d+1, an+1, tsn, tnn);some[d][i] = 0, none[d][nn++] = v;}}void work(){ans = 0;for(int i = 0; i < n; ++i) some[1][i] = i+1;dfs(1, 0, n, 0);}int main(){scanf("%d %d", &n, &k);for(int i = 1; i <= n; ++i)for(int j = 1; j <= n; ++j){int x; scanf("%d", &x);mp[i][j] = x;}work();double res = k*k*1.0*(ans-1)/ans/2;printf("%.10f\n", res);return 0;}


继续加油~

原创粉丝点击