有 N 种 atom,每种 atom 与不同于自身的 atom 碰撞会产生一定能量,且有一方消失,
atom 自身不能和自身碰撞,有一个矩阵表示他们之间碰撞所产生的能量,
A[i][j] = x 代表 i 和 j 碰撞,产生 x能量且 j 消失,问最多能产生多少能量?
#include <iostream>
#include <cstring>
using namespace std;
int arr[11][11];
int dp[1 << 10];
int main(){
int atom_num = 0;
while( cin >> atom_num ){
if( atom_num == 0 )
break;
for( int row = 0; row < atom_num; ++row )
for( int col = 0; col < atom_num; ++col )
cin >> arr[row][col];
memset( dp, 0, sizeof( dp ) );
const int bits = 1 << atom_num;
int ans = 0;
for( int bit = 0; bit < bits; ++bit ){
for( int alive = 0; alive < atom_num; ++alive ){
if( bit & ( 1 << alive ) )
continue;
for( int death = 0; death < atom_num; ++death ){
if( bit & ( 1 << death ) || alive == death )
continue;
dp[bit + ( 1 << death )] = max( dp[bit + ( 1 << death )], dp[bit] + arr[alive][death] );
ans = max( ans, dp[bit + ( 1 << death )] );
}
}
}
cout << ans << endl;
}
return 0;
}