1 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
#define max(a,b) a>b?a:b
#define LL long long
#define inf 0x3f3f3f3f
int a[], b[], num[<<];
LL dp[][<<]; // dp[x][y] x表示数组最后一个元素,y表示当前状态
int n;
int getnum(int x){
int ret=;
while(x){
ret++;
x=x&(x-);
}
return ret;
}
void init(){
for(int i=;i<(<<);++i)
num[i]=getnum(i);
}
int main()
{
int t, i, j, k, cas=;
init();
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(i=;i<n;++i)
scanf("%d%d",&a[i],&b[i]);
for(i=;i<n;++i){
for(j=;j<(<<n);++j)
dp[i][j]=-inf;
}
for(i=;i<n;++i){
if(b[i]==-||b[i]==)
dp[i][<<i]=;
}
for(i=;i<(<<n);++i){
for(j=;j<n;++j){
if(i&(<<j)==) continue;
if(dp[j][i]==-inf) continue;
for(k=;k<n;++k){
if(j==k) continue;
if(i&(<<k)) continue;
if(b[k]==-||b[k]==num[i]){
dp[k][i|(<<k)]=max(dp[k][i|(<<k)],
dp[j][i]+a[j]*a[k]);
}
}
}
}
LL ans=-inf;
for(i=;i<n;++i){
ans=max(ans,dp[i][(<<n)-]);
}
printf("Case #%d:\n",cas++);
printf("%lld\n",ans);
}
return ;
}

hdu5691 Sitting in Line(状压dp)的更多相关文章

  1. hdu 5691 Sitting in Line 状压dp

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5691 题解: 和tsp用的状压差不多,就是固定了一些访问顺序. dp[i][j]表示前cnt个点中布 ...

  2. hdu_5691_Sitting in Line(状压DP)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5691 题意:中文,不解释 题解:设dp[i][j]表示当前状态为i,以第j个数为末尾的最忧解,然后dp ...

  3. hdu 5691 Sitting in line 状压动归

    在本题中,n<=16n<=16n<=16, 不难想到可以将所选数字的编号进行状态压缩. 定义状态 dp[S][j]dp[S][j]dp[S][j],其中 SSS 代表当前所选出的所有 ...

  4. HDU5691 Sitting in Line【状压DP】

    HDU5691 Sitting in Line 题意: 给出\(n\)个数字,有些数字的位置固定了,现在要求把所有没固定的数字放在一个位置,使得任意相邻两个位置的数字的相乘的和最大 题解: \(n\) ...

  5. 【BZOJ1688】[Usaco2005 Open]Disease Manangement 疾病管理 状压DP

    [BZOJ1688][Usaco2005 Open]Disease Manangement 疾病管理 Description Alas! A set of D (1 <= D <= 15) ...

  6. 【POJ3254】Corn Fields 状压DP第一次

    !!!!!!! 第一次学状压DP,其实就是运用位运算来实现一些比较,挺神奇的.. 为什么要发“!!!”因为!x&y和!(x&y)..感受一下.. #include <iostre ...

  7. codeforces Diagrams & Tableaux1 (状压DP)

    http://codeforces.com/gym/100405 D题 题在pdf里 codeforces.com/gym/100405/attachments/download/2331/20132 ...

  8. ZOJ3802 Easy 2048 Again (状压DP)

    ZOJ Monthly, August 2014 E题 ZOJ月赛 2014年8月 E题 http://acm.zju.edu.cn/onlinejudge/showProblem.do?proble ...

  9. CF453B Little Pony and Harmony Chest (状压DP)

    CF453B CF454D Codeforces Round #259 (Div. 2) D Codeforces Round #259 (Div. 1) B D. Little Pony and H ...

  10. poj3254 Corn Fields (状压DP)

    http://poj.org/problem?id=3254 Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissio ...

随机推荐

  1. (转)Shell学习之Shift的用法

    Shell学习之Shift的用法 原文:https://www.cnblogs.com/sunfie/p/5943753.html 位置参数可以用shift命令左移.比如shift 3表示原来的$4现 ...

  2. [原创]II7/IIS8屏蔽YisouSpider蜘蛛

    来源:http://www.0531s.com/content-46-1927014-1.html YisouSpider蜘蛛抓取网页能够导致CPU暴涨,影响其他蜘蛛和用户的访问,多次实验后,发现II ...

  3. CRLF注入攻击

      原理:http数据包通过\r\n\r\n来分开http header何http body 实现:首先这种攻击发生在应用层,且发生在服务器返回给我们的http reponse没有经过敏感字符的过滤, ...

  4. 网页设计,Access入门 2010,数学

    网页设计(表格) 创建表格:插入---表格---设置表格大小---确定.(按Ctrl键可多选单元格) 插入图片在表格:光标在单元格---插入---图像---选择图像---确定. 表格属性:属性(屏幕下 ...

  5. C# WinForm拖入文件到窗体,得到文件路径

    private void textBox1_DragDrop(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataForma ...

  6. Offic转换pdf 之asposeDLL插件

    //excel转换 Workbook workbook = new Workbook(HttpContext.Current.Server.MapPath(docpath + "/" ...

  7. POS开发问题 - 多个弹出框的实现

    业务场景如下图: 页面出现提示框:    点击确定,隐藏上面的弹出框, 继续弹出提示:  点击确定隐藏上面的弹出框,继续弹出下面提示: 点击确定隐藏上面的弹出框,继续弹出下面提示: 点击确定,跳转页面 ...

  8. python+selenium第一个脚本

    #coding=utf-8from selenium import webdriverfrom selenium.webdriver.common.keys import Keysimport tim ...

  9. rollback

    savepoint test; rollback to savepoint test;

  10. selenium 服务器端运行命令

    cd C:\Users\kfa_wangchao\Downloadsjava -jar selenium-server-standalone-2.37.0.jarcmd=getNewBrowserSe ...