状压dp Codeforces Beta Round #8 C
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的更多相关文章
- 暴力/DP Codeforces Beta Round #22 (Div. 2 Only) B. Bargaining Table
题目传送门 /* 题意:求最大矩形(全0)的面积 暴力/dp:每对一个0查看它左下的最大矩形面积,更新ans 注意:是字符串,没用空格,好事多磨,WA了多少次才发现:( 详细解释:http://www ...
- 数论+dp Codeforces Beta Round #2 B
http://codeforces.com/contest/2/problem/B 题目大意:给你一个n*n的矩形,问从(1,1)出发到(n,n),把图中经过的所有的数字都乘在一起,最后这个数字有多少 ...
- 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 ...
- Codeforces Beta Round #16 E. Fish (状压dp)(概率dp)
Codeforces Beta Round #16 (Div. 2 Only) E. Fish 题目链接:## 点击打开链接 题意: 有 \(n\) 条鱼,每两条鱼相遇都会有其中一只吃掉对方,现在给你 ...
- Educational Codeforces Round 13 E. Another Sith Tournament 状压dp
E. Another Sith Tournament 题目连接: http://www.codeforces.com/contest/678/problem/E Description The rul ...
- Codeforces Round #321 (Div. 2) D. Kefa and Dishes(状压dp)
http://codeforces.com/contest/580/problem/D 题意: 有个人去餐厅吃饭,现在有n个菜,但是他只需要m个菜,每个菜只吃一份,每份菜都有一个欢乐值.除此之外,还有 ...
- Codeforces Round #531 (Div. 3) F. Elongated Matrix(状压DP)
F. Elongated Matrix 题目链接:https://codeforces.com/contest/1102/problem/F 题意: 给出一个n*m的矩阵,现在可以随意交换任意的两行, ...
- Codeforces Round #363 LRU(概率 状压DP)
状压DP: 先不考虑数量k, dp[i]表示状态为i的概率,状态转移方程为dp[i | (1 << j)] += dp[i],最后考虑k, 状态表示中1的数量为k的表示可行解. #incl ...
- 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 ...
随机推荐
- HDU 2212 DFS
Problem Description A DFS(digital factorial sum) number is found by summing the factorial of every d ...
- Node.js:全局对象
概要:本篇博客主要介绍了node.js中的全局对象. 在JavaScript中,通常window是全局对象,而node.js中的全局对象是global,所有全局变量(除了global本身之外)都是gl ...
- button,input type=button按钮在IE和w3c,firefox浏览器区别
在项目中遇到一个问题,是关于点击button按钮会自动刷新的问题.查阅了资料,做以下的整理: button,input type=button按钮在IE和w3c,firefox浏览器区别如下:当在IE ...
- 你应该知道的CSS文字大小单位PX、EM、PT
这里引用的是Jorux的“95%的中国网站需要重写CSS”的文章, 题目有点吓人,但是确实是现在国内网页制作方面的一些缺陷.我一直也搞不清楚px与em之间的关系和特点,看过以后确实收获很大.平时都是用 ...
- AVFoundation--AVCaptureSession
// // ViewController.m // AVFountionCamera // // Created by ZhuYi on 16/5/3. // Copyright © 2016年 DD ...
- 【Sort】希尔排序
希尔排序(ShellSort),缩小增量排序,使用希尔增量时最坏运行时间O(n^2),不同的增量会对运行时间产生显著影响. void shellsort(int *nums,int n) { int ...
- javascript基础(五)函数
原文http://pij.robinqu.me/ 通过call和apply间接调用函数(改变this) call 和 apply带有多个参数,call和apply把当前函数的this指向第一个参数给定 ...
- 苹果dock效果
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- 图的连通性:有向图强连通分量-Tarjan算法
参考资料:http://blog.csdn.net/lezg_bkbj/article/details/11538359 上面的资料,把强连通讲的很好很清楚,值得学习. 在一个有向图G中,若两顶点间至 ...
- 3.通过js代码设置css样式
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...