题目

传送门:QWQ

分析

这种题不会做 吃枣药丸。。。。。

想到状压已经经过的点。

然后更新时枚举两个点加进去。

复杂度$  {O(2^n \times n^2)}$。

凉凉。

真正的做法是每一个状态只要找到一组解就break。这样可以省掉一层n。

大致上就像lrj紫书的dp例题一样,反正这个点都要选,那就先选了他。

还不是很懂这个神奇的break。。。。。

代码

 #include <bits/stdc++.h>
using namespace std;
const int maxn=, INF=1e9; int dp[<<maxn], dis[maxn][maxn], pre[<<maxn];
int x[maxn], y[maxn]; int sqr(int x){ return x*x; } int main(){
scanf("%d%d",&x[],&y[]);
int n; scanf("%d",&n);
for(int i=;i<=n;i++) {
scanf("%d%d",&x[i],&y[i]);
}
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
dis[i][j]=dis[j][i]=sqr(x[i]-x[j])+sqr(y[i]-y[j]);
}
} memset(dp,,sizeof(dp)); dp[]=;
for(int S=;S<(<<n);S++){
if(dp[S]>INF) continue;
for(int i=;i<=n;i++){
if(S&(<<i-)) continue;
for(int j=;j<=n;j++){
if(S&(<<j-)) continue; int prenum=dp[S|<<(i-)|<<(j-)];
dp[S|<<(i-)|<<(j-)]=min(dp[S|<<(i-)|<<(j-)], dp[S]+dis[][i]+dis[][j]+dis[i][j]);
if(prenum>dp[S|<<(i-)|<<(j-)]){
pre[S|<<(i-)|<<(j-)]=S;
}
}
break;
}
} printf("%d\n",dp[(<<n)-]);
int now=(<<n)-; //从所有都齐的状态开始逆推
while ( now!= )
{
printf( "0 " );
int update=now^pre[now];
for ( int i=; i<=n; i++ )
if ( update&<<i- )
printf( "%d ", i ); //输出本次拿的哪些物品
now=pre[now];
}
puts("");
}

【Codeforces】CF 8 C Looking for Order(状压dp)的更多相关文章

  1. Codeforces Beta Round #8 C. Looking for Order 状压dp

    题目链接: http://codeforces.com/problemset/problem/8/C C. Looking for Order time limit per test:4 second ...

  2. codeforces 8C. Looking for Order 状压dp

    题目链接 给n个物品的坐标, 和一个包裹的位置, 包裹不能移动. 每次最多可以拿两个物品, 然后将它们放到包里, 求将所有物品放到包里所需走的最小路程. 直接状压dp就好了. #include < ...

  3. 【题解】codeforces 8c Looking for Order 状压dp

    题目描述 Lena喜欢秩序井然的生活.一天,她要去上大学了.突然,她发现整个房间乱糟糟的--她的手提包里的物品都散落在了地上.她想把所有的物品都放回她的手提包.但是,这里有一点问题:她一次最多只能拿两 ...

  4. Codeforces Gym 100610 Problem K. Kitchen Robot 状压DP

    Problem K. Kitchen Robot Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10061 ...

  5. Educational Codeforces Round 13 E. Another Sith Tournament 状压dp

    E. Another Sith Tournament 题目连接: http://www.codeforces.com/contest/678/problem/E Description The rul ...

  6. Codeforces 1225G - To Make 1(bitset+状压 dp+找性质)

    Codeforces 题目传送门 & 洛谷题目传送门 还是做题做太少了啊--碰到这种题一点感觉都没有-- 首先我们来证明一件事情,那就是存在一种合并方式 \(\Leftrightarrow\) ...

  7. CF1103D Codeforces Round #534 (Div. 1) Professional layer 状压 DP

    题目传送门 https://codeforces.com/contest/1103/problem/D 题解 失去信仰的低水平选手的看题解的心路历程. 一开始看题目以为是选出一些数,每个数可以除掉一个 ...

  8. 【Codeforces】CF 165 E Compatible Numbers(状压dp)

    题目 传送门:QWQ 分析 很难想到方向,但有方向了就很easy了. 我们如何减少不必要的计算? 如果我们知道了$ 100111 $的相容的数,$ 100101 $的相容数和他是完全一样的. 我们就靠 ...

  9. Codeforces 279D The Minimum Number of Variables 状压dp

    The Minimum Number of Variables 我们定义dp[ i ][ mask ]表示是否存在 处理完前 i 个a, b中存者 a存在的状态是mask 的情况. 然后用sosdp处 ...

  10. codeforces#580 D. Kefa and Dishes(状压dp)

    题意:有n个菜,每个菜有个兴奋值,并且如果吃饭第i个菜立即吃第j个菜,那么兴奋值加ma[i][j],求吃m个菜的最大兴奋值,(n<=18) 分析:定义dp[status][last],statu ...

随机推荐

  1. python打包成.exe

    pyuic5 mainwindow.ui -o test.py pip install pyinstaller pyinstaller -F -w ***.py https://blog.csdn.n ...

  2. iOS被开发者遗忘在角落的NSException-其实它很强大

    NSException是什么? 最熟悉的陌生人,这是我对NSException的概述,为什么这么说呢?其实很多开发者接触到NSException的频率非常频繁,但很多人都不知道什么是NSExcepti ...

  3. Java Iterator的一般用法

    Iterator(迭代器) 迭代器是一种设计模式,它是一个对象,它可以遍历并选择序列中的对象,而开发人员不需要了解该序列的底层结构.迭代器通常被称为“轻量级”对象,因为创建它的代价小. Java中的I ...

  4. Vue拖拽组件

    vue开发公众号项目,***产品需要添加一个新的功能.拖拽功能.一听简单.百度上轮子挺多,直接拉一个过来用着就行.然鹅...兴奋之余,却失望至极.东西很多,没有一个能使得.你让我失望,那我就让你绝望. ...

  5. js 获取客户端mac地址

    js 获取客户端mac地址 javascript获取客户端网卡MAC地址和IP地址和计算机名 nodesj如何获得客户端的mac地址呢? 浏览器获取MAC地址 不限浏览器的mac地址取得的几种办法 I ...

  6. [LeetCode&Python] Problem 461. Hamming Distance

    The Hamming distance between two integers is the number of positions at which the corresponding bits ...

  7. ubuntu12.04 alternate win7 双系统安装

    ubuntu alternate的安装比desktop复杂一点,因为alternate的安装过程有个步骤是检测cd-rom,如果你是刻盘安装,自然没问题,但是,现在的安装一般是将系统刻到U盘里,或者在 ...

  8. Luogu 3245 大数

    Luogu 3245 大数 开始就想 \(10\) 进制 \(hash\) ,\(Hash(r)\equiv Hash(l-1)\cdot 10^{r-l+1}\) ,感觉没什么美妙的性质啊... 然 ...

  9. LeetCode-Microsoft-Remove K Digits

    Given a non-negative integer num represented as a string, remove k digits from the number so that th ...

  10. php 的交互命令行

    php 的交互命令行 使用过 python 都知道 python 可以使用交互命令. 如下图: 但是 执行 php 显示这个是什么鬼? 按回车和加分号都没用,这是什么原因? 其实是因为使用 php 交 ...