ZOJ 4257 MostPowerful(状压DP,简单)
2016-05-18 14:44
483 查看
F - Most Powerful
Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld
& %llu
Submit Status
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 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
【题目大意】不超过10种气体,两两之间相互碰撞可以产生一定的能量,如a碰b,那么b气体就消失,自身不能碰自身,问最后所能得到的最大能量。
原代码链接:http://blog.csdn.net/accry/article/details/6607703
题解:感觉这个题是我做状态压缩的几个题中最简单的了,这存图用a数组就可以了,也不用处理a数组(有的求路径的题要用弗洛伊德处理原数组),因为碰撞是不可逆的。
接着就用s开始从0枚举状态到1<<N-1,还要注意去掉不可能的情况!!
AC代码:
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int MAXN=10;
const int MAX_S=1<<10;
int a[MAXN+1][MAXN+1];
int dp[MAX_S];
int N;
int main()
{
while(cin>>N,N)
{
for (int i=0; i<N; i++)
for (int j=0; j<N; j++)
scanf("%d",&a[i][j]);
memset(dp,0,sizeof(dp));
int full=1<<N;
for (int s=0; s<full; s++)/**< 这块是状态的枚举,必须小于1<<N,
且从0开始,并且,一般都用S做循环变量*/
{
for (int i=0; i<N; i++)
{
if ((s&(1<<i))) continue;//去掉i自己的情况
for (int j=0; j<N; j++)
{
if (i==j) continue;//去掉相同的情况
if ((s&(1<<j))) continue;//去掉j自己的情况
int newS=s+(1<<j);
dp[newS]=max(dp[newS],dp[s]+a[i][j]);
}
}
}
int ans = 0;
for(int s = 0; s < full ; ++s)
ans = max(ans,dp[s]);
printf("%d\n",ans);
}
}
Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld
& %llu
Submit Status
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 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
【题目大意】不超过10种气体,两两之间相互碰撞可以产生一定的能量,如a碰b,那么b气体就消失,自身不能碰自身,问最后所能得到的最大能量。
原代码链接:http://blog.csdn.net/accry/article/details/6607703
题解:感觉这个题是我做状态压缩的几个题中最简单的了,这存图用a数组就可以了,也不用处理a数组(有的求路径的题要用弗洛伊德处理原数组),因为碰撞是不可逆的。
接着就用s开始从0枚举状态到1<<N-1,还要注意去掉不可能的情况!!
AC代码:
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int MAXN=10;
const int MAX_S=1<<10;
int a[MAXN+1][MAXN+1];
int dp[MAX_S];
int N;
int main()
{
while(cin>>N,N)
{
for (int i=0; i<N; i++)
for (int j=0; j<N; j++)
scanf("%d",&a[i][j]);
memset(dp,0,sizeof(dp));
int full=1<<N;
for (int s=0; s<full; s++)/**< 这块是状态的枚举,必须小于1<<N,
且从0开始,并且,一般都用S做循环变量*/
{
for (int i=0; i<N; i++)
{
if ((s&(1<<i))) continue;//去掉i自己的情况
for (int j=0; j<N; j++)
{
if (i==j) continue;//去掉相同的情况
if ((s&(1<<j))) continue;//去掉j自己的情况
int newS=s+(1<<j);
dp[newS]=max(dp[newS],dp[s]+a[i][j]);
}
}
}
int ans = 0;
for(int s = 0; s < full ; ++s)
ans = max(ans,dp[s]);
printf("%d\n",ans);
}
}
相关文章推荐
- 简单的四则运算
- 数的奇偶性
- ACMer博客瀑布流分析
- ACM程序设计大赛题目分类
- 计算字符串最后一个单词长度
- ACM网址
- 1272 小希的迷宫
- 1272 小希的迷宫
- hdu 1250 大数相加并用数组储存
- 矩阵的乘法操作
- 蚂蚁爬行问题
- 蚂蚁爬行问题
- 求两个数的最大公约数【ACM基础题】
- 打印出二进制中所有1的位置
- 杭电题目---一只小蜜蜂
- HDOJ 1002 A + B Problem II (Big Numbers Addition)
- 初学ACM - 半数集(Half Set)问题 NOJ 1010 / FOJ 1207
- 初学ACM - 组合数学基础题目PKU 1833
- POJ ACM 1001
- ACM题库以及培养策略