暴力搜索dfs,注意要利用递归来减少计算总通信量的次数。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
#define maxn 21
int map[maxn][maxn], ans = 0;
bool in[maxn];
int n;
void dfs(int now, int sum)
{
if (now > n)
{
if (sum > ans)
ans = sum;
return;
}
in[now] = true;
int add = 0;
for (int i = 0; i < now; i++)
if (!in[i])
add += map[i][now];
dfs(now + 1, add + sum);
in[now] = false;
add = 0;
for (int i = 0; i < now; i++)
if (in[i])
add += map[i][now];
dfs(now + 1, add + sum);
}
int main()
{
//freopen("t.txt", "r", stdin);
scanf("%d", &n);
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
scanf("%d", &map[i][j]);
dfs(0, 0);
printf("%d\n", ans);
return 0;
}