HDU3362-Fix

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?
 

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.
 

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;
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值