Description
Recently, researchers on Mars has discovered N powerful atoms. All of them is different. These atoms has some properties. When the These atoms collide, one of the them disappears and a lot of power are produced. Researchers know the every-atoms perform when collided and the power every both atoms can produce.
You-to-write a program-to-make it powerful, which means the sum of power produced during all the collides is Maximal.
Input
There is multiple cases. The first line of all case have an integer N (2 <= N <=), which means there is N atoms:a1, A2, ..., an. Then N lines follow. There is N integers in each line. The j-th integer on the i-th line was the power produced when Ai and AJ collide with AJ gone. All integers is positive and not larger than 10000.
The last case was followed by a 0 on one line.
There would be no more than cases including no more than a large cases that N is 10.
Output
Output The maximal power these N atoms can produce in a line for each case.
Sample Input
2
0 4
1 0
3
0 20 1
12 0 1
1 10 0
0
Sample Output
4
22
This topic for each particle existence and disappearance of two States, the total number of natural states have 2^n species.
For a certain state, if with ordinary expression, naturally have dp[0][1][0] ... [1] (where 1 means presence, 0 means vanishing), because each particle has only 0 and 12 values, where the binary is used for state compression.
The next consideration for a state 10001011, if the 11th to become 0, it is natural to consider the particle and the existing particles to collide, and then find the maximum value can be reached. Let's remember that this value is Getmax (state, now) (where states are the current binary status, which is the one that is converted from 1 to 0).
Then consider the state transition equation, for the state 10001, must be the state 11001 or 10101 or 10011 to convert, natural for state, can be a 0 bit into the status of 1 forestate conversion. So:
Dp[state] = Max{dp[forestate] + getmax (forestate, now)}
This is solved using a memory search. Initial state M = (1<<n) ^0.
Code:
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring>using namespace std; int N, m;int p[15][15];int dp[1500];void Input () {memset (DP, 0, sizeof (DP)); for (int i = 1; I <= n; ++i) for (int j = 1; j <= N; ++j) scanf ("%d", &p[i][j]); M = (m<<n) ^0;} int Getmax (int state, Int. now) {int Max = 0; for (int i = 0; i < n; ++i) if (state& (1<<i)) max = max (max, P[i+1][now]); return Max;} void Dfs (int state) {if (state = = M) {Dp[state] = 0; Return } int Max = 0, t; for (int i = 0; i < n; ++i) {if ((state& (1<<i)) = = 0) {if (dp[state| ( 1<<i)] = = 0) Dfs (state| ( 1<<i)); t = dp[state| ( 1<<i)] + getmax (state, i+1); max = max (max, T); }} Dp[state] = Max;} int work () {int ans = 0; for (int i = 0; i < n; ++i) {Dfs (1<<i); ans = max (ans, dp[1<<i]); } return ans; int main () {//freopen ("test.in", "R", stdin); while (scanf ("%d", &n)! = EOF && N) {Input (); printf ("%d\n", Work ()); } return 0;}
ACM Learning process-zoj3471 Most powerful (DP && state compression && memory search && bit operations)