1019 Most Powerful 状压DP-NP问题
链接:https://ac.nowcoder.com/acm/contest/25022/1019
来源:牛客网
题目描述
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.输入描述:
There are multiplecases. The first line of each case has an integer N (2 <= N <= 10), whichmeans there are N atoms: A1, A2, ... , AN.Then N lines follow. There are N integers in each line. The j-th integer on thei-th line is the power produced when Ai and Aj collidewith Aj gone. All integers are positive and not larger than 10000.
The last case isfollowed by a 0 in one line.
There will be no morethan 500 cases including no more than 50 large cases that N is 10.
输出描述:
Output the maximalpower these N atoms can produce in a line for each case.
分析
每两种气体相撞都有不同情况,所以是np问题。
设f[i] 表示状态i 最大的能量
第k位为0,表示第k中气体还没消失,为1表示消失了。
f[i | (1 << (k - 1) ] = max(f[i | ( 1 << (k - 1) + a[j][k] ) 表示用第j 种气体去撞第k种气体
//-------------------------代码---------------------------- //#define int ll const int N = 11; int n,m; int f[1<<11]; int a[N][N];; void solve() { ms(f,0); fo(i,1,n) { fo(j,1,n) { cin>>a[i][j]; } } fo(i,0,(1<<n) - 1) { fo(j,1,n) { if(i & (1 << (j - 1))) continue; fo(k,1,n) { if(i & (1 << (k - 1) )) continue; f[i| (1 <<(k-1))] = max(f[i | ( 1 << ( k - 1 ) )],f[i] + a[j][k]); } } } cout<<f[(1 << n ) - 1] <<endl; } signed main(){ AC(); clapping();TLE; // int t;cin>>t;while(t -- ) while(cin>>n,n) solve(); // {solve(); } return 0; } /*样例区 */ //------------------------------------------------------------