Fix
Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1038 Accepted Submission(s): 350
Problem Description
There are a few points on a plane, and some are fixed on the plane, some are not. We want to connect these points by some sticks so that all the points are fixed on the plane. Of course, we want to know the minimum length of the sum of the sticks.
As in the images, the black points are fixed on the plane and red ones are not, which need to be fixed by sticks.
All the points in the left image have been fixed. But the middle one is not, which we need add one stick to fix those four points (the right image shows that stick). Triangle is steady, isn’t it?

As in the images, the black points are fixed on the plane and red ones are not, which need to be fixed by sticks.
All the points in the left image have been fixed. But the middle one is not, which we need add one stick to fix those four points (the right image shows that stick). Triangle is steady, isn’t it?
Input
The input consists of multiply test cases. The first line of each test case contains one integer, n (1 <= n <= 18), which is the number of points. The next n lines, each line consists of three integers, x, y, c (0 <= x, y < 100). (x, y) indicate the coordinate of one point; c = 1 indicates this point is fixed; c = 0 indicates this point is not fixed. You can assume that no two points have the same coordinate.
The last test case is followed by a line containing one zero, which means the end of the input.
The last test case is followed by a line containing one zero, which means the end of the input.
Output
Output the minimum length with six factional digits for each test case in a single line. If you cannot fix all the points, then output “No Solution”.
Sample Input
4 0 0 1 1 0 1 0 1 0 1 1 0 3 0 0 1 1 1 0 2 2 0 0
Sample Output
4.414214 No Solution
Source
题意:题目给出n(n <= 18)个点的二维坐标,并说明某些点是被固定了的,其余则没固定,要求添加一些边,使得还没被固定的点变成固定的,若不能使所有点固定则输出No Solution
解题思路:由于n很小,而且固定点的顺序没有限制,可以用状态压缩DP。当一个没固定的点和两个固定了的点连接后,该点就被间接固定了(三角形的稳定性质)。无论是直接固定还是间接固定的点,都可以供以后的点用于固定。若有有两点以上是固定则一定可以将所有点固定。因此,对于当前需要固定的点,在已经是固定状态的点中选出两个距离当前点最小的,这就保证了局部最优,从起始状态开始转移,最后判断能否到达最终目标状态就可以了。
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <set>
using namespace std;
int a[50],b[50],c[50];
double dp[1000000];
int main()
{
int n;
while(~scanf("%d",&n)&&n)
{
int k=0,cnt=0;
memset(dp,0,sizeof dp);
for(int i=0;i<n;i++)
{
scanf("%d %d %d",&a[i],&b[i],&c[i]);
k=k|(c[i]<<i);
if(c[i]) cnt++;
}
if(n==1&&c[0]) {printf("0.000000\n");continue;}
if(n==1&&!c[0]) {printf("No Solution\n");continue;}
if(cnt<2) {printf("No Solution\n");continue;}
int m=1<<n;
for(int i=k+1;i<m;i++)
{
int flag=1;
for(int j=0;j<n;j++)
if(c[j]&&!((1<<j)&i)) flag=0;
if(!flag) continue;
double mi=999999999;
for(int j=0;j<n;j++)
{
if(((1<<j)&i)&&!c[j])
{
int kk=i-(1<<j);
for(int p=0;p<n;p++)
{
if((1<<p)&kk)
{
for(int q=p+1;q<n;q++)
{
if((1<<q)&kk)
mi=min(mi,dp[kk]+1.0*sqrt((a[j]-a[p])*(a[j]-a[p])+(b[j]-b[p])*(b[j]-b[p]))+1.0*sqrt((a[j]-a[q])*(a[j]-a[q])+(b[j]-b[q])*(b[j]-b[q])));
}
}
}
}
}
dp[i]=mi;
}
printf("%lf\n",dp[m-1]);
}
return 0;
}