状压DP uvalive 6560
// 状压DP uvalive 6560
// 题意:相邻格子之间可以合并,合并后的格子的值是之前两个格子的乘积,没有合并的为0,求最大价值
// 思路:
// dp[i][j]:第i行j状态下的值
// j:0表示不合并,1表示向下合并
// 一开始输入要修改一下,然后滚动数组优化 #include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cmath>
#include <map>
#include <queue>
using namespace std;
#define LL long long
typedef pair<int,int> pii;
const int inf = 0x3f3f3f3f;
const int MOD = ;
const int N = ;
const int maxx = ;
#define clc(a,b) memset(a,b,sizeof(a))
const double eps = 0.025;
void fre() {freopen("in.txt","r",stdin);}
void freout() {freopen("out.txt","w",stdout);}
inline int read() {int x=,f=;char ch=getchar();while(ch>''||ch<'') {if(ch=='-') f=-; ch=getchar();}while(ch>=''&&ch<='') {x=x*+ch-'';ch=getchar();}return x*f;} int n;
int g[N][];
int dp[][];
bool check(int u,int f){
for(int i=;i<;i++){
if(u&(<<i)&&f&(<<i)) return false;
}
return true;
} int cal(int r,int u,int f){
int ans=;
for(int i=;i<;i++){
if(f&(<<i)) ans+=g[r][i]*g[r-][i];
}
int t=u|f;
int tem1=,tem2=;
if(!(t&)&&!(t&)) tem1=g[r][]*g[r][];
if(!(t&)&&!(t&)) tem2=g[r][]*g[r][];
ans+=max(tem1,tem2);
return ans;
}
int main(){
int cas=;
while(~scanf("%d",&n),n){
for(int i=;i<;i++){
for(int j=;j<=n;j++){
scanf("%d",&g[j][i]);
}
}
memset(dp[],,sizeof(dp[]));
int p=;
int ans=-inf;
for(int i=;i<=n;i++){
int pre=p;
p=p^;
memset(dp[p],,sizeof(dp[p]));
for(int j=;j<;j++){
for(int k=;k<;k++){
if(check(j,k)){
dp[p][j]=max(dp[p][j],dp[pre][k]+cal(i,j,k));
}
}
if(i==n){
ans=max(ans,dp[p][j]);
}
}
}
printf("Case %d: ",cas++);
printf("%d\n",ans);
}
return ;
}
状压DP uvalive 6560的更多相关文章
- UVAlive 6560 - The Urge to Merge(状压dp)
LA 6560 - The Urge to Merge option=com_onlinejudge&Itemid=8&page=show_problem&problem=45 ...
- UVaLive 6625 Diagrams & Tableaux (状压DP 或者 DFS暴力)
题意:给一个的格子图,有 n 行单元格,每行有a[i]个格子,要求往格子中填1~m的数字,要求每个数字大于等于左边的数字,大于上边的数字,问有多少种填充方法. 析:感觉像个DP,但是不会啊...就想暴 ...
- BZOJ 1087: [SCOI2005]互不侵犯King [状压DP]
1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3336 Solved: 1936[Submit][ ...
- nefu1109 游戏争霸赛(状压dp)
题目链接:http://acm.nefu.edu.cn/JudgeOnline/problemShow.php?problem_id=1109 //我们校赛的一个题,状压dp,还在的人用1表示,被淘汰 ...
- poj3311 TSP经典状压dp(Traveling Saleman Problem)
题目链接:http://poj.org/problem?id=3311 题意:一个人到一些地方送披萨,要求找到一条路径能够遍历每一个城市后返回出发点,并且路径距离最短.最后输出最短距离即可.注意:每一 ...
- [NOIP2016]愤怒的小鸟 D2 T3 状压DP
[NOIP2016]愤怒的小鸟 D2 T3 Description Kiana最近沉迷于一款神奇的游戏无法自拔. 简单来说,这款游戏是在一个平面上进行的. 有一架弹弓位于(0,0)处,每次Kiana可 ...
- 【BZOJ2073】[POI2004]PRZ 状压DP
[BZOJ2073][POI2004]PRZ Description 一只队伍在爬山时碰到了雪崩,他们在逃跑时遇到了一座桥,他们要尽快的过桥. 桥已经很旧了, 所以它不能承受太重的东西. 任何时候队伍 ...
- bzoj3380: [Usaco2004 Open]Cave Cows 1 洞穴里的牛之一(spfa+状压DP)
数据最多14个有宝藏的地方,所以可以想到用状压dp 可以先预处理出每个i到j的路径中最小权值的最大值dis[i][j] 本来想用Floyd写,无奈太弱调不出来..后来改用spfa 然后进行dp,这基本 ...
- HDU 1074 Doing Homework (状压dp)
题意:给你N(<=15)个作业,每个作业有最晚提交时间与需要做的时间,每次只能做一个作业,每个作业超出最晚提交时间一天扣一分 求出扣的最小分数,并输出做作业的顺序.如果有多个最小分数一样的话,则 ...
随机推荐
- React如何性能调优
一. 二.调优例子 <!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset=&q ...
- float和decimal执行效率 (只是代码 没有分析—)
float版: public static void getSmallFramPoint() { string framString ="Row,"+"Colum,&qu ...
- 8天学通MongoDB——第八天 驱动实践
作为系列的最后一篇,得要说说C#驱动对mongodb的操作,目前驱动有两种:官方驱动和samus驱动,不过我个人还是喜欢后者, 因为提供了丰富的linq操作,相当方便. 官方驱动:https://gi ...
- POJ 2065 SETI (高斯消元 取模)
题目链接 题意: 输入一个素数p和一个字符串s(只包含小写字母和‘*’),字符串中每个字符对应一个数字,'*'对应0,‘a’对应1,‘b’对应2.... 例如str[] = "abc&quo ...
- 函数fil_node_create
/*******************************************************************//** Appends a new file to the c ...
- ha_innobase::general_fetch
/***********************************************************************//** Reads the next or previ ...
- WebApp开发框架Ionic+AngularJS+Cordova
目前的手机APP有三类:原生APP.WebAPP.HybridApp:HybridApp结合了前两类APP各自的优点,越来越流行. Ionic Ionic是一个新的.可以使用HTML5构建混合移动应用 ...
- android开发中eclipse里xml开发的自动提示和使用帮助快捷键提示
Eclipse Android 代码自动提示功能 Eclipse for android 设置代码提示功能 打 开 Eclipse 依次选择 Window > Preferences > ...
- Java [Leetcode 337]House Robber III
题目描述: The thief has found himself a new place for his thievery again. There is only one entrance to ...
- jrtplib跨网络通讯NAT穿透问题解决方法
前几篇文章讲了使用jrtplib在Android和pc端进行通讯的方法 在实际项目中,手机端和pc端一般不会在同一个子网内,两者之间联络可能要走路由器之类的NAT(网络地址转换 Network Add ...