Zoj 4257 most powerful (State compression DP)

Source: Internet
Author: User
Most powerful
Time limit:2 seconds Memory limit:65536 KB

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 ajgone. 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.

Sample Input

2
0 4
1 0
3
0 20 1
12 0 1
1 10 0
0

Sample output

4
22

Author:Gao, Yuan
Contest:Zoj monthly, February 2011

 

 

 

 

Simple state compression DP,

The status is in binary format. 0 indicates that the State is not eliminated. 1 indicates that the State has been eliminated.

DP [I] indicates the maximum value obtained when the status is I. Then, we will continue to enumerate the two involved in the collision.

(I thought of a wrong idea at the beginning, 555555555)

 

 //  ========================================================== ==========================================  //  Name: zoj. cpp  //  Author: //  Version:  //  Copyright: Your copyright notice  //  Description: Hello world in C ++, ANSI-style  //  ========================================================== ==========================================  # Include <Iostream> # Include <Stdio. h> # Include < String . H> # Include <Algorithm> Using   Namespace  STD;  Int DP [ 1 < 12  ];  Int G [ 12 ] [ 12  ];  Int  Main (){  //  Freopen ("in.txt", "r", stdin ); //  Freopen ("out.txt", "W", stdout );      Int  N;  While (Scanf ( "  % D  " , & N) = 1 && N ){  For ( Int I = 0 ; I <n; I ++ )  For ( Int J = 0 ; J <n; j ++ ) Scanf (  "  % D  " ,& G [I] [J]); memset (DP, - 1 , Sizeof  (DP); DP [  0 ] = 0  ;  Int TOT = (1 < N );  Int Ans = 0  ;  For ( Int I = 0 ; I <tot; I ++ ){  If (DP [I] =- 1 ) Continue  ; Ans = Max (ANS, DP [I]); For ( Int J = 0 ; J <n; j ++ )  For ( Int K = 0 ; K <n; k ++ )  If (J! = K & (I &( 1 <J) = 0 & (I &( 1 <K) = 0 ) {DP [I | ( 1 <J)] = max (DP [I | ( 1 <J)], DP [I] + G [k] [J]);  //  DP [I | (1 <k)] = max (DP [I | (1 <k)], DP [I] + G [J] [k]);  } Printf (  "  % D \ n  "  , ANS );}  Return   0 ;} 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.