HDU 3362 Fix(状压dp)

Posted -凡-尘

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 3362 Fix(状压dp)相关的知识,希望对你有一定的参考价值。

Fix

Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1037    Accepted Submission(s): 349

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

题目大意:把动点固定最少需要多长的木棒。

题解:状态压缩+dp

#include <iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
double inf=100000000000.0;
double dis[300000];
int n;
int start,target;
struct node
{
    double x,y;
    bool fix;
}p[20];
double cal(int a,int b)
{
  return sqrt((double)((p[a].x-p[b].x)*(p[a].x-p[b].x)+(p[a].y-p[b].y)*(p[a].y-p[b].y)));
}
double work(int k,int t)
{
    double dis[20];
    int l=0;
    for(int i=0;i<n;i++)
     if (k&(1<<i))   dis[l++]=cal(i,t); //判断第i个点是否固定
    if (l<2) return -1;
    sort(dis,dis+l);//sort的第一个参数首地址,第二个参数尾地址,排序范围为“[,)”
    return dis[0]+dis[1];
}
int main()
{
   while(~scanf("%d",&n))
   {
       if (n==0) break;
       start=0;
       target=0;
       for(int i=0;i<n;i++)
       {
          scanf("%lf%lf%d",&p[i].x,&p[i].y,&p[i].fix);
          start=start|(p[i].fix<<i);
          target=target|(1<<i);
       }
       for(int i=0;i<=target;i++) dis[i]=inf;
       dis[start]=0;
       for(int k=start;k<=target;k++)
       {
//if (dis[k]==inf) continue; 这句最好加上
for(int i=0;i<n;i++) if ( !(k&(1<<i)) )//判断第i个点是否可以移动,如果可以继续做 { double res=work(k,i); if(res>0) dis[k|(1<<i)]=min(dis[k|(1<<i)],dis[k]+res); } } if (dis[target]==inf) printf("No Solution\n"); else printf("%.6lf\n",dis[target]); } return 0; }

 







以上是关于HDU 3362 Fix(状压dp)的主要内容,如果未能解决你的问题,请参考以下文章

HDU4057 Rescue the Rabbit(AC自动机+状压DP)

HDU 5765 Bonds(状压DP)

HDU 1074 Doing Homework 状压DP

HDU 1074 Doing Homework(状压DP)

HDU 3001 Travelling 3进制状压dp

HDU 4628 Pieces(状压DP)题解