题目链接:

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

C. Looking for Order

time limit per test:4 secondsmemory limit per test:512 megabytes

问题描述

Girl Lena likes it when everything is in order, and looks for order everywhere. Once she was getting ready for the University and noticed that the room was in a mess — all the objects from her handbag were thrown about the room. Of course, she wanted to put them back into her handbag. The problem is that the girl cannot carry more than two objects at a time, and cannot move the handbag. Also, if he has taken an object, she cannot put it anywhere except her handbag — her inherent sense of order does not let her do so.

You are given the coordinates of the handbag and the coordinates of the objects in some Сartesian coordinate system. It is known that the girl covers the distance between any two objects in the time equal to the squared length of the segment between the points of the objects. It is also known that initially the coordinates of the girl and the handbag are the same. You are asked to find such an order of actions, that the girl can put all the objects back into her handbag in a minimum time period.

输入

The first line of the input file contains the handbag's coordinates xs, ys. The second line contains number n (1 ≤ n ≤ 24) — the amount of objects the girl has. The following n lines contain the objects' coordinates. All the coordinates do not exceed 100 in absolute value. All the given positions are different. All the numbers are integer.

输出

In the first line output the only number — the minimum time the girl needs to put the objects into her handbag.

In the second line output the possible optimum way for Lena. Each object in the input is described by its index number (from 1 to n), the handbag's point is described by number 0. The path should start and end in the handbag's point. If there are several optimal paths, print any of them.

样例输入

1 1
3
4 3
3 4
0 0

样例输出

32
0 1 2 0 3 0

题意

给你垃圾桶的位置和垃圾的位置,你每次能从垃圾桶出发捡一到两个垃圾然后回到垃圾桶,问如何规划使得捡垃圾所花时间最短(时间是以两点距离平方为基准)

题解

状压dp,有点像最优顶点配对问题的算法来处理,时间复杂度为n*2^n。

代码

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printftypedef long long  LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII;const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-8;
const double PI = acos(-1.0);//start----------------------------------------------------------------------const int maxn=24;int dp[1<<maxn],n;
int pre[1<<maxn];
struct Node{int a,b;bool type;
}nds[1<<maxn];PII pt[maxn];int dis(int i,int j){int a=pt[i].X-pt[j].X;int b=pt[i].Y-pt[j].Y;return a*a+b*b;
}int main() {scf("%d%d",&pt[0].X,&pt[0].Y);scf("%d",&n);for(int i=1;i<=n;i++){scf("%d%d",&pt[i].X,&pt[i].Y);}clr(dp,0x7f);dp[0]=0;clr(pre,-1);for(int stat=1;stat<(1<<n);stat++){for(int i=0;i<n;i++){if(stat&(1<<i)){///第一个单独不匹配if(dp[stat]>dp[stat^(1<<i)]+2*dis(0,i+1)){dp[stat]=dp[stat^(1<<i)]+2*dis(0,i+1);pre[stat]=stat^(1<<i);nds[stat].a=i+1;nds[stat].type=0;}dp[stat]=min(dp[stat],dp[stat^(1<<i)]+2*dis(0,i+1));///第一个和后面的某一个匹配for(int j=0;j<n;j++){if(j!=i&&stat&(1<<j)){int tmp=dp[stat^(1<<i)^(1<<j)]+dis(0,i+1)+dis(i+1,j+1)+dis(0,j+1);if(dp[stat]>tmp){dp[stat]=tmp;pre[stat]=stat^(1<<i)^(1<<j);nds[stat].a=i+1; nds[stat].b=j+1;nds[stat].type=1;}}}//这个减枝非常关键!和最优顶点配对的做法一样break;}}}VI ans;ans.pb(0);int p=(1<<n)-1;while(p!=0){if(nds[p].type==0){ans.pb(nds[p].a);}else{ans.pb(nds[p].a);ans.pb(nds[p].b);}ans.pb(0);p=pre[p];}reverse(all(ans));prf("%d\n",dp[(1<<n)-1]);rep(i,0,ans.sz()-1) prf("%d ",ans[i]);prf("%d\n",ans[ans.sz()-1]);return 0;
}//end-----------------------------------------------------------------------

转载于:https://www.cnblogs.com/fenice/p/5846852.html

Codeforces Beta Round #8 C. Looking for Order 状压dp相关推荐

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

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

  2. Codeforces Round #585 (Div. 2) E. Marbles 状压dp + 逆序对

    传送门 文章目录 题意: 思路: 题意: 思路: 考虑数列最终的状态一定是相同颜色在一起,所以我们发现他的颜色是有顺序的!显然可以用状压dpdpdp来枚举颜色的顺序,但是又有问题了,你怎么确定当前这个 ...

  3. Codeforces Beta Round #5 C. Longest Regular Bracket Sequence 栈/dp

    C. Longest Regular Bracket Sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.c ...

  4. Codeforces Beta Round #52 (Div. 2) D. Changing a String DP输出方案

    https://codeforces.com/contest/56/problem/D 就是编辑距离的加强版,需要输出方案,那么状态转移方程应该比较常规了,设 d p [ i ] [ j ] dp[i ...

  5. Codeforces Beta Round #95 (Div. 2) 部分解题报告 (dp,组合数,)

    做这样的比赛既考快速编码的能力,还有快速思维的能力.本人很弱,跌了rating..加油!!!.. 第一题上来就把题意理解错了..粗心啊..直接模拟着做就行:1:如果字符串全是大写字母就进行大小写转换: ...

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

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

  7. 【Codeforces】Gym 101173B Bipartite Blanket 霍尔定理+状压DP

    题意 给一张$n\times m$二分图,带点权,问有多少完美匹配子集满足权值和大于等于$t$ 这里有一个结论:对于二分图$\mathbb{A}$和$\mathbb{B}$集合,如果子集$A \in ...

  8. Codeforces Beta Round #17 D. Notepad (数论 + 广义欧拉定理降幂)

    Codeforces Beta Round #17 题目链接:点击我打开题目链接 大概题意: 给你 \(b\),\(n\),\(c\). 让你求:\((b)^{n-1}*(b-1)\%c\). \(2 ...

  9. Codeforces Beta Round #5 B. Center Alignment 模拟题

    B. Center Alignment 题目连接: http://www.codeforces.com/contest/5/problem/B Description Almost every tex ...

最新文章

  1. 学会这八个技术,你离 BAT 大厂不远了
  2. 瑞典皇家理工学院工程实例:Sound localization
  3. html的分类与特点
  4. Java中的程序设计模式--单例与多例
  5. exchange 2010申请分配证书服务提示:证书无效,不可用于exchange server
  6. 工作实践 之 Google Guava 工具集的使用 ,提高效率
  7. win11资源管理器历史搜索记录如何删除 Windows11禁用资源管理器历史搜索记录的设置方法
  8. 解析mysqlbinlog日志_关于mysql-binlog日志解析框架
  9. dnf 跨服 服务器 位置,dnf跨区怎么跨_dnf国服跨区表_快吧游戏
  10. 最新版校园招聘进大厂系列----------(5)百度篇 -----未完待续
  11. 求ax2+bx+c=0方程的解,要求(1) a=0,不是二次方程。(2) b2-4ac=0,有两个相同的实根。(3)b2-4ac>0,有两个不等的实根。(4)b2-4ac<有两个共轭的复根
  12. 通过谷歌骇客语法搜索后台:_书评:我们的骇客并拥有
  13. 广发样样行提额技巧分享
  14. 蘑菇街php面试,蘑菇街面试
  15. 2020秋招 C++软件开发/游戏开发 面试知识整理汇总
  16. Android利用zxing生成二维码,识别二维码,中间填充图片超详细、超简易教程
  17. Windows : bat脚本
  18. AI:2020年6月24日北京智源大会演讲分享之知识智能专题论坛——11:30-12:00唐杰 教授《CogDL:An Extensive Research Toolkit for Deep Le》
  19. Python中判断输入数据的类型
  20. java通过进程名称杀进程_根据进程名杀死进程 -kill进程名

热门文章

  1. 《Java核心技术 卷Ⅱ 高级特性(原书第10版)》一3.6.2 使用StAX解析器
  2. node-glob学习
  3. Introduction to Microservices
  4. C#通用权限管理-程序安全检查,这些你一定要考虑到位
  5. 从交换机分析网络风暴
  6. android 画中画模式自定义,Android 8.0 Oreo 画中画模式
  7. java 生产者消费者同步_经典线程同步问题(生产者消费者)--Java实现
  8. java jmenu 监听_Java中用得比较顺手的事件监听
  9. raspberry pi_在Raspberry Pi上试用Docker
  10. 优必选能开放软硬件源代码吗_开放式硬件为何胜出