http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3471
Recently, researchers on Mars have discovered N powerful atoms. All of them are different. These atoms have some properties. When two of these atoms collide, one of them disappears and a lot of power is produced. Researchers know the way every two atoms perform when collided and the power every two atoms can produce.
You are to write a program to make it most powerful, which means that the sum of power produced during all the collides is maximal.
Input
There are multiple cases. The first line of each case has an integer N (2 <= N <= 10), which means there are N atoms: A1, A2, ... , AN. Then N lines follow. There are N integers in each line. The j-th integer on the i-th line is the power produced when Ai and Aj collide with Aj gone. All integers are positive and not larger than 10000.
The last case is followed by a 0 in one line.
There will be no more than 500 cases including no more than 50 large cases that N is 10.
Output
Output the maximal power these N atoms can produce in a line for each case.
题意:i与j相遇的话,j消失,且产生v[i][j]的能量,求n个点能产生的最大能量。
思路:设f[state] 状态为state时的最大能量,则f[state] = max(f[state],f[state']+a[i][j])
#include<bits/stdc++.h>
using namespace std;
int n,v[10][10],f[1<<10];
void dp()
{
int maxx=0;
memset(f,-1,sizeof(f));
f[(1<<n)-1]=0;
for(int s=(1<<n)-2;s>=0;s--)
{
for(int i=0;i<n;i++)if(!((1<<i)&s))
for(int j=0;j<n;j++)if((1<<j)&s)
{
f[s]=max(f[s],f[s^(1<<i)]+v[j][i]);
}
maxx=max(maxx,f[s]);
}
cout<<maxx<<"\n";
}
int main()
{
// freopen("input.in","r",stdin);
while(cin>>n&&n)
{
for(int i=0;i<n;i++)for(int j=0;j<n;j++)cin>>v[i][j];
dp();
}
return 0;
}