CodeForces - 8C:Looking for Order

C. Looking for Order
time limit per test
4 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

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.

Output

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.

Examples
input
0 0
2
1 1
-1 1
output
8
0 1 2 0 
input
1 1
3
4 3
3 4
0 0
output
32
0 1 2 0 3 0 

思路:状压DP,不过有个地方要优化。

#include<bits/stdc++.h>
using namespace std;
struct lenka
{
	int x,y;
}a[30];
int d[1<<24];
int dis[30][30];
int pre[1<<24];
int ans[100],MAX;
int sx,sy,n;
int main()
{
	scanf("%d%d%d",&sx,&sy,&n);
	a[n].x=sx,a[n].y=sy;
	for(int i=0;i<n;i++)scanf("%d%d",&a[i].x,&a[i].y);
	for(int i=0;i<=n;i++)
	{
		for(int j=0;j<=n;j++)dis[i][j]=(a[i].x-a[j].x)*(a[i].x-a[j].x)+(a[i].y-a[j].y)*(a[i].y-a[j].y);
	}
	memset(pre,0,sizeof pre);
	memset(d,0x3f3f3f3f,sizeof d);
	MAX=0;
	d[0]=0;
	pre[0]=-1;
	for(int i=0;i<(1<<n);i++)
	{
		if(d[i]==0x3f3f3f3f)continue;
		for(int j=0;j<n;j++)
		{
			if(i&(1<<j))continue;
			for(int k=0;k<n;k++)// j==k,j!=k,这2种情况都可以
			{
				if(i&(1<<k))continue;
				if(d[i|(1<<j)|(1<<k)]>d[i]+dis[k][n]+dis[j][n]+dis[j][k]) 
				{
					d[i|(1<<j)|(1<<k)]=(d[i]+dis[k][n]+dis[j][n]+dis[j][k]);
					pre[i|(1<<j)|(1<<k)]=i; //记录路径 
				}
			}
			break;  //此处一定要及时跳出循环,因为是2个物品一取或一个一取,只要遍历了一次k之后,就可以break了 
		}
	}
	printf("%d\n",d[(1<<n)-1]);
	int nex=(1<<n)-1,tag,sum,k;
	ans[MAX++]=0;
	while(pre[nex]!=-1)
	{
		tag=0,sum=nex^pre[nex],k=1;
		while(sum)
		{
			if(sum%2)tag=1,ans[MAX++]=k;
			sum/=2;
			k++;
		}
		if(tag)ans[MAX++]=0;
		nex=pre[nex];
	}
	printf("%d",ans[0]);
	for(int i=1;i<MAX;i++)printf(" %d",ans[i]);
	printf("\n");
	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值