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.
Input
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.
OutputIn 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.
Examples0 0 2 1 1 -1 1
8 0 1 2 0
1 1 3 4 3 3 4 0 0
32 0 1 2 0 3 0
状压DP思路:由于各个点是固定的,所以拿取顺序并不影响最终结果。用二进制数表示当前的状态,dp【i】表示该状态下最小的价值。
1.预处理一次性拿一个或两个的价值,存入cost数组。
2.注意每次从第一个未拿取的位置开始枚举,只需枚举一轮,这样时间复杂度从O(1<<bit*n*n)降为O(1<<bit*n),减少了计算重复状态的时间。
3.每次更新成功后将当前信息存入前驱数组pre,输出时可以运用栈结构(stack)适当简化代码。
Code1:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define N 25
#define INF 1000000000
int x0,y0;
int n;
struct CO {
int x;
int y;
}a[30],a0;
int dp[1 << N];
int low[1 << N];
int pre[1 << N];
void yuch(){
low[0] = 1;
for(int i = 0; i < (1 << n); i++){
low[i] = ((i&1) == 0 ? 1 : (low[i >> 1] + 1));
}
}
int dis(CO a, CO b){
int ans = 0;
return ans = (a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y);
}
int main() {
scanf("%d %d",&x0,&y0);
a0.x = x0;
a0.y = y0;
scanf("%d",&n);
yuch();
for(int i = 0; i < n; i++){
scanf("%d%d", &a[i].x, &a[i].y);
}
for(int i = 0; i < (1<<n); i++){
dp[i] = INF;
pre[i] = 0;
}
dp[0] = 0;
for(int i = 0; i < (1 << n) - 1; i++){
for(int j = 0; j < n; j++){
if(j == low[i] - 1){//只拿一个
if((i & (1 << (low[i] - 1))) != 0)continue;
dp[i + (1 << (low[i] - 1))] = min(dp[i] + dis(a[low[i] - 1],a0) * 2, dp[i + (1 << (low[i]-1))]);
if(dp[i + (1 << (low[i] - 1))] == dp[i] + dis(a[low[i] - 1],a0) * 2){
pre[i + (1 << (low[i] - 1))] = i;
}
}
else {//两个
if(((i & (1 << j)) != 0) || ((i & (1 <<(low[i] - 1))) != 0))continue;
dp[i + (1 << (low[i] - 1)) +(1 << j)] = min(dp[i] + dis(a[low[i] - 1], a0) + dis(a[low[i] - 1], a[j]) + dis(a[j], a0) , dp[i + (1 << (low[i]-1)) +(1 << j)]);
if(dp[i + (1 << (low[i] - 1)) +(1 << j)] == dp[i] + dis(a[low[i] - 1], a0) + dis(a[low[i] - 1], a[j]) + dis(a[j], a0)){
pre[i + (1 << (low[i] - 1)) +(1 << j)] = i;
}
}
}
}
int pro[30];
int ind = (1 << n) - 1;
int fs = 1;
pro[0] = ind;
while(pre[ind] != 0){
pro[fs++] = pre[ind];
ind = pre[ind];
}
pro[fs] = 0;
printf("%d\n0 ",dp[(1 << n) - 1]);
for(int i = fs; i > 0 ; i--){
for(int j = 0; j < n; j++){
if((((1 << j) & pro[i]) == 0) && (((1 << j) & pro[i - 1]) != 0))printf("%d ",j + 1);
}
printf("0 ");
}
printf("\n");
return 0;
}
DFS思路:
由于数据量较小,故可以考虑深搜,思路与状压DP大致相同。
从这题的摸爬滚打也可以看出,算法之间往往是可以相互转换的,最重要的是底层的思想。
Code2:
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <stack>
#include <climits>
using namespace std;
#define INF 1000000000
const int bit=24;
int sx,sy;
int n;
int ans;
int cur;
int pre[1<<bit];
int cost[1<<bit];
int vis[bit];
int value[1<<bit];
void DFS(int x,int cur,int p){
if(cur<value[x]){
value[x]=cur;
pre[x]=p;
}
else{
return;
}
if(x==(1<<n)-1){
ans=min(ans,cur);
return ;
}
for(int i=0;i<n;i++){
if(!(x&(1<<i))){
DFS(x+(1<<i),cur+cost[1<<i],1<<i);
for(int j=i+1;j<n;j++){
if(!(x&(1<<j))){
DFS(x+(1<<i)+(1<<j),cur+cost[(1<<i)|(1<<j)],(1<<i)+(1<<j));
}
}
break;
}
}
}
struct Node{
int x,y;
}S[bit];
int pow(int x){
return x*x;
}
int main(){
cin>>sx>>sy;
cin>>n;
for(int i=0;i<n;i++){
cin>>S[i].x>>S[i].y;
}
for(int i=0;i<n;i++){
cost[1<<i]=2*(pow(S[i].x-sx)+pow(S[i].y-sy));
}
for(int i=0;i<n-1;i++){
for(int j=i+1;j<n;j++){
cost[(1<<i)|(1<<j)]=(pow(S[i].x-sx)+pow(S[i].y-sy)+pow(S[j].x-sx)+pow(S[j].y-sy)+pow(S[i].x-S[j].x)+pow(S[i].y-S[j].y));
}
}
//for(int i=0;i<(1<<n);i++) cout<<"cost["<<i<<"]="<<cost[i]<<endl;
ans=INF;
for(int i=0;i<(1<<bit);i++){
value[i]=INF;
}
memset(vis,0,sizeof vis);
DFS(0,0,0);
int x=(1<<n)-1;
cout<<ans<<endl;
stack<int> ans;
ans.push(0);
while(x){
for(int i=0;i<n;i++){
if(pre[x]&(1<<i)) ans.push(1+i);
}
x=x-pre[x];
ans.push(0);
}
while(!ans.empty()){
cout<<ans.top()<<" ";
ans.pop();
}
}