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. 理解Nodejs中的事件轮询机制

    我在看<了不起的Nodejs>一书,阻塞与非阻塞IO那一章我来回看了N遍,然后...还是没太看懂..于是我找到了这篇日志,写的是真的有点好啊..潸然泪下.. 原文:http://www.r ...

  2. JavaScript事件响应的基础语法总结

    1.onclick是鼠标单击事件,当在网页上单击鼠标时,就会发生该事件.同时onclick事件调用的程序块就会被执行,通常与按钮一起使用 //例子 <html> <head> ...

  3. 谷歌浏览器js debug

  4. XML 树结构

    XML 树结构 XML 文档形成了一种树结构,它从"根部"开始,然后扩展到"枝叶". 一个 XML 文档实例 XML 文档使用简单的具有自我描述性的语法: &l ...

  5. MongoDB高级操作

    参考MongoDB菜鸟教程 一.$type操作符 MongoDB 中可以使用的类型如下表所示: 类型 数字 备注 Double 1   String 2   Object 3   Array 4   ...

  6. Nimbus<一>Storm系列(五)架构分析之Nimbus启动过程

    启动流程图 mk-assignments 功能:对当前集群中所有Topology进行新一轮的任务调度. 实现源码路径: \apache-storm-0.9.4\storm-core\src\clj\b ...

  7. LeetCode OJ 123. Best Time to Buy and Sell Stock III

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  8. Openjudge-计算概论(A)-苹果和虫子

    描述 你买了一箱n个苹果,很不幸的是买完时箱子里混进了一条虫子.虫子每x小时能吃掉一个苹果,假设虫子在吃完一个苹果之前不会吃另一个,那么经过y小时你还有多少个完整的苹果? 输入输入仅一行,包括n,x和 ...

  9. 用mybatis生成插件自动生成配置文件

    1.在当前的maven项目的pom.xml中添加插件 <build> <plugins> <plugin> <groupId>org.mybatis.g ...

  10. unity 退到桌面的 OnApplicationPause

    void OnApplicationFocus( bool isFocus ) { // Debug.Log("--------OnApplicationPause---" + i ...