【状压dp】Most Powerful

[ZOJ3471]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 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.

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

题目大意:给你一些元素,这些元素碰撞后会产生其中消失的物品的能量(你可以自己决定哪一个消失),求最后剩1个元素的最大能量释放值

试题分析:比较基础的一道状压dp      貌似在dp中我只能说状压dp有基础题,好弱QAQ

     dp[S]表示序列消到状态为S的时候的最大释放能量值。

     可得出如下转移方程(因为是消失,所以应该从(1<<N)-1到1枚举): dp[S]=max(dp[S],dp[S+(1<<(k-1))]+make[j][k]);

     其中k代表消失的能量,j代表剩下的能量,前提是S包含j,不包含k,k!=j

 

代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;

inline int read(){
	int x=0,f=1;char c=getchar();
	for(;!isdigit(c);c=getchar()) if(c==‘-‘) f=-1;
	for(;isdigit(c);c=getchar()) x=x*10+c-‘0‘;
	return x*f;
}
const int MAXN=100001;
const int INF=999999;
int N,M;
int dp[MAXN];
int e[1001][1010];

int main(){
    N=read();
    while(1){
    	if(!N) break;
    	memset(dp,0,sizeof(dp));
    	for(int i=1;i<=N;i++){
    		for(int j=1;j<=N;j++)
    		    e[i][j]=read();
		}
		int ans=0;
		for(int i=(1<<N)-2;i>=1;i--){
			for(int j=1;j<=N;j++){
				if(!((i>>(j-1))&1)) continue;
				for(int k=1;k<=N;k++){
					if(k==j||((i>>(k-1))&1)) continue;
					dp[i]=max(dp[i],dp[i+(1<<(k-1))]+e[j][k]);
				}
			}
		}
		for(int i=1;i<=N;i++) ans=max(ans,dp[1<<(i-1)]);
		printf("%d\n",ans);
		N=read();
	}
}
时间: 2024-10-25 19:00:39

【状压dp】Most Powerful的相关文章

ZOJ 3471 Most Powerful(状压DP)

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 at

状压DP [ZOJ 3471] Most Powerful

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

ZOJ 3471 Most Powerful(状压DP)

Description 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

zoj3471Most Powerful 状压dp

#include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdlib> #include <list> #include <

ZOJ 3471 Most Powerful 状压DP

水题,一维的DP,表示还剩哪些atom的时候能获得的最大能量 #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <set> #include <vector> #include <string> #include <queue> #include <deque> #include <

ZOJ3305Get Sauce 状压DP,

状压DP的题目留个纪念,首先题意一开始读错了,搞了好久,然后弄好了,觉得DFS可以,最后超时,修改了很久还是超时,没办法看了一下n的范围,然后觉得状压可以,但是没有直接推出来,就记忆化搜索了一下,可是一直错,莫名奇妙,然后没办法看了一下题解,发现了下面这个比较好的方法,然后按照这个方程去推,然后敲,也是WA了好多把,写的太搓了,没人家的清楚明了,唉~也算是给自己留个纪念,状压一直做的都不太好~唉~还好理解了, 参考了  http://blog.csdn.net/nash142857/articl

poj 2411 Mondriaan&#39;s Dream(状压DP)

Mondriaan's Dream Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 12232   Accepted: 7142 Description Squares and rectangles fascinated the famous Dutch painter Piet Mondriaan. One night, after producing the drawings in his 'toilet series

(状压dp)uva 10817 Headmaster&#39;s Headache

题目地址 1 #include <bits/stdc++.h> 2 typedef long long ll; 3 using namespace std; 4 const int MAX=1e5+5; 5 const int INF=1e9; 6 int s,m,n; 7 int cost[125]; 8 //char sta[MAX]; 9 string sta; 10 int able[125]; 11 int dp[125][1<<8][1<<8]; 12 in

HDU5816 Hearthstone(状压DP)

题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5816 Description Hearthstone is an online collectible card game from Blizzard Entertainment. Strategies and luck are the most important factors in this game. When you suffer a desperate situation an