http://codeforces.com/contest/8/problem/C

题目大意:给你一个坐标系,给你一个人的目前的坐标(该坐标也是垃圾桶的坐标),再给你n个垃圾的坐标,这个人要捡完所有的垃圾,而且每次最多只能捡两个,然后把他扔到垃圾桶里面去。问这个人捡完所有垃圾所需要的最短的路程是多少?(路程=两个坐标之间连线距离的平方)

思路:貌似是简单的状压dp?

我们枚举一下1<<n就好了,然后每次都取最高位和其他的某一个进行匹配即可(而不用取其他位,因为其他位在之前就已经枚举过了)。

//看看会不会爆int!数组会不会少了一维!
//取物问题一定要小心先手胜利的条件
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define ALL(a) a.begin(), a.end()
#define pb push_back
#define mk make_pair
#define fi first
#define se second
#define haha printf("haha\n")
const int maxn = ;
int dp[ << maxn], par[ << maxn];
pair<int, int> girl, a[maxn + ];
int n; inline int get_high(int val){///1需要右移几位
int pos = ;
while (val){
val >>= ; pos++;
}
return pos - ;
} inline int dis(int x, int y){
if (x == -)
return (girl.fi - a[y].fi) * (girl.fi - a[y].fi) + (girl.se - a[y].se) * (girl.se - a[y].se);
return (a[x].fi - a[y].fi) * (a[x].fi - a[y].fi) + (a[x].se - a[y].se) * (a[x].se - a[y].se);
} int main(){
///int high = get_high(4); pos = 2 4 = 100
scanf("%d%d", &girl.fi, &girl.se);
cin >> n;
for (int i = ; i < n; i++){
int x, y; scanf("%d%d", &x, &y);
a[i] = mk(x, y);
}
memset(dp, 0x3f, sizeof(dp));
memset(par, -, sizeof(par));
dp[] = ;
for (int i = ; i < ( << n); i++){
int high = get_high(i);///最高位是第几位
dp[i] = min(dp[i], dp[i ^ ( << high)] + dis(-, high) * );
par[i] = i ^ ( << high);
for (int j = ; j < high; j++){
if (i & ( << j)) {
int tmp = dp[i ^ ( << j) ^ ( << high)] + dis(j, high) + dis(-, j) + dis(-, high);
if (dp[i] > tmp){
dp[i] = tmp; par[i] = i ^ ( << j) ^ ( << high);
}
}
}
}
printf("%d\n", dp[( << n) - ]);
int tmp = ( << n) - ;
printf("0 ");
for (int i = par[tmp]; i != -; i = par[i]){
int val = tmp ^ i;
int pos = ;
while (val){
if (val & ) printf("%d ", pos + );
val >>= ; pos++;
}
printf("0 ");
tmp = i;
}
cout << endl;
return ;
}

状压dp Codeforces Beta Round #8 C的更多相关文章

  1. 暴力/DP Codeforces Beta Round #22 (Div. 2 Only) B. Bargaining Table

    题目传送门 /* 题意:求最大矩形(全0)的面积 暴力/dp:每对一个0查看它左下的最大矩形面积,更新ans 注意:是字符串,没用空格,好事多磨,WA了多少次才发现:( 详细解释:http://www ...

  2. 数论+dp Codeforces Beta Round #2 B

    http://codeforces.com/contest/2/problem/B 题目大意:给你一个n*n的矩形,问从(1,1)出发到(n,n),把图中经过的所有的数字都乘在一起,最后这个数字有多少 ...

  3. 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 ...

  4. Codeforces Beta Round #16 E. Fish (状压dp)(概率dp)

    Codeforces Beta Round #16 (Div. 2 Only) E. Fish 题目链接:## 点击打开链接 题意: 有 \(n\) 条鱼,每两条鱼相遇都会有其中一只吃掉对方,现在给你 ...

  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 Round #321 (Div. 2) D. Kefa and Dishes(状压dp)

    http://codeforces.com/contest/580/problem/D 题意: 有个人去餐厅吃饭,现在有n个菜,但是他只需要m个菜,每个菜只吃一份,每份菜都有一个欢乐值.除此之外,还有 ...

  7. Codeforces Round #531 (Div. 3) F. Elongated Matrix(状压DP)

    F. Elongated Matrix 题目链接:https://codeforces.com/contest/1102/problem/F 题意: 给出一个n*m的矩阵,现在可以随意交换任意的两行, ...

  8. Codeforces Round #363 LRU(概率 状压DP)

    状压DP: 先不考虑数量k, dp[i]表示状态为i的概率,状态转移方程为dp[i | (1 << j)] += dp[i],最后考虑k, 状态表示中1的数量为k的表示可行解. #incl ...

  9. Codeforces Round #321 (Div. 2) D. Kefa and Dishes 状压dp

    题目链接: 题目 D. Kefa and Dishes time limit per test:2 seconds memory limit per test:256 megabytes 问题描述 W ...

随机推荐

  1. mac安装软件系列

    1,mac安装homebrew,注意不能用root权限安装 #ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew ...

  2. 【第四篇】Volley修改之GsonRequest

    json解析工具类的引入,这里引用lite马天宇的解析json的工具类: public class GsonImpl extends Json { private Gson gson = new Gs ...

  3. 纯CSS实现斜角

    今天看了看腾讯的七周年时光轴,发现这个斜角的CSS,研究了半天提出下面代码可以直接实现斜角,不是CSS3哦,那个就太容易了 -webkit-transform:rotate(10deg); 倾斜度后再 ...

  4. Node.js:全局对象

    概要:本篇博客主要介绍了node.js中的全局对象. 在JavaScript中,通常window是全局对象,而node.js中的全局对象是global,所有全局变量(除了global本身之外)都是gl ...

  5. jQuery插件实现左右无缝轮播

    前段时间学习jQuery的时候,在网上找了个SuperSlide插件,做轮播图demo,感觉对于新人而言,还是挺容易上手的,代码量也少. 直接贴代码吧. 1.HTML <!DOCTYPE htm ...

  6. (sql占位符)Remember that ordinal parameters are 1-based!

    第一次出现是 将第一个问号(?)的位置设为1了,其实是从0开始的. 第二次出现是 sql中的 问号 写成了 中文输入法下的问号,这个太难发现了,搞了半天才发现.然后改成英式问号就可以了. 每天进步一点 ...

  7. putty完全使用手册--多窗口---git提交---连接数据库--自动日志显示

    01 putty dev 192.168.3.38 02 cd /home/gongfu/tripb/cranedev02.1 git status git add git commit git pu ...

  8. GUI自绘_其中左边树状菜单控件风格灵感来源于城市博物馆的壁灯效果。

    GUI DEMO 下面都是去年做的演示DEMO,到目前为止,除了专门做界面库的公司,暂时还没有看到别人做的效果比我这个更好的. 下图在第一张图中有个错误,看出来了没有呢? 就是项目核算那儿,不应该是B ...

  9. apicloud 初学

    html5:在创建html时为了防止页面缩放等不兼容效果,要创建个viewport <meta name="viewport" content="maximum-s ...

  10. NTFS 读写高手进阶 - Windows 格式硬盘 Mac存文件 开启 ...(转载)

    常识: 硬盘格式: FAT32 - WIndows 硬盘分区格式, 有点通用性高, 缺点不支持单个大于 4G 的文件. exFAT - Windows 硬盘分区格式, 兼容性低. 稳定性不如 FAT3 ...