Codeforces 8C Looking for Order(状压DP)
来源:互联网 发布:centos 安装lamp环境 编辑:程序博客网 时间:2024/06/08 13:17
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.
0 021 1-1 1
80 1 2 0
1 134 33 40 0
320 1 2 0 3 0
题目大意:
有平面上有n个物品,一个人没次最多带两个物品,问这个人从起点出发,把所有物品拿到起点最少走过平方距离。
解题思路:
简单的转压dp,不过这里拿物品的顺序是无所谓的,我们就可以利用这个特性减少状态数,可以强行要求没次拿第一个物品的时候,前面的已经全部拿完。
AC代码:
#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <cstdlib>#include <cmath>#include <ctime>#include <vector>#include <queue>#include <stack>#include <deque>#include <string>#include <map>#include <set>#include <list>using namespace std;#define INF 0x3f3f3f3f#define LL long long#define fi first#define se second#define mem(a,b) memset((a),(b),sizeof(a))const int MAXN=24;const int MAXS=1<<MAXN;int y[MAXN+1],x[MAXN+1],N,S;int dp[MAXS],last[MAXS],op[MAXS];int G[MAXN+1][MAXN+1];vector<int> ans;void init(){ mem(dp,0x3f); mem(last,-1); mem(op,0);}void out(int s){ if(~last[s]) out(last[s]); if(op[s]) { if(op[s]>=100) ans.push_back(op[s]/100); ans.push_back(op[s]%100); ans.push_back(0); }}int main(){ scanf("%d%d",&y[0],&x[0]); scanf("%d",&N); S=1<<N; init(); for(int i=1;i<=N;++i) scanf("%d%d", &y[i], &x[i]); for(int i=0;i<=N;++i) for(int j=i;j<=N;++j) G[i][j]=G[j][i]=(y[i]-y[j])*(y[i]-y[j])+(x[i]-x[j])*(x[i]-x[j]); dp[0]=0; for(int s=0;s<S;++s) if(dp[s]<INF) { for(int i=0;i<N;++i) if(!(s&(1<<i))) { int next_s=s|(1<<i), next_val=dp[s]+2*G[0][i+1]; if(next_val<dp[next_s]) { dp[next_s]=next_val; last[next_s]=s; op[next_s]=i+1; } for(int j=i+1;j<N;++j) if(!(s&(1<<j))) { int next_s=s|(1<<i)|(1<<j), next_val=dp[s]+G[i+1][j+1]+G[0][i+1]+G[0][j+1]; if(next_val<dp[next_s]) { dp[next_s]=next_val; last[next_s]=s; op[next_s]=(i+1)*100+(j+1); } } break;//利用无序强行要求顺序来剪枝 } } printf("%d\n",dp[S-1]); out(S-1); printf("0 "); for(int i=0;i<ans.size();++i) printf("%d%c", ans[i], i==ans.size()-1?'\n':' '); return 0;}
- Codeforces 8C Looking for Order(状压DP)
- Codeforces 8C Looking for Order(状态压缩DP)
- #8 C. Looking for Order (状压dp + 路径记忆)
- codeforces 8C - Looking for Order 状态DP
- codeforces 8C - Looking for Order 状态DP
- Codeforces Beta Round #8 C. Looking for Order 状压DP,路径记录
- CodeForces 8C-Looking for Order
- CF 8C Looking for Order(状压dp)
- CodeForces 8C - Looking for Order 状态压缩DP..DP降低复杂度
- Codeforces Beta Round #8 C. Looking for Order CF8C
- CF 8C Looking for Order
- Looking for Order 压缩DP
- CodeForces8C Looking for Order【状压】
- Codeforces 350D - Looking for Owls
- [Codeforces 543C] Remembering Strings (状压DP)
- codeforces 698 C. LRU (概率与期望+状压DP)
- looking for internship ...
- looking for thunder link
- nyoj 题目12:喷水装置(二)贪心算法
- S
- hdu 6055 Regular polygon
- c++ STL vector如何删除
- [BZOJ]1717 [Usaco2006 Dec]Milk Patterns 二分答案
- Codeforces 8C Looking for Order(状压DP)
- GridView长按短按冲突
- Java的一些小总结
- Comparable与Comparator的区别
- DAY45 数据库--MySQL2
- $("#id")[0]问题
- 子树/最近公共祖先
- unicode,ansi,utf-8,unicode big endian编码区别 --篇2
- linux ssh 免密登录