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

HDU 3362 Fix(状压dp)的更多相关文章

  1. 2014 Super Training #1 B Fix 状压DP

    原题: HDU 3362 http://acm.hdu.edu.cn/showproblem.php?pid=3362 开始准备贪心搞,结果发现太难了,一直都没做出来.后来才知道要用状压DP. 题意: ...

  2. HDU 4284Travel(状压DP)

    HDU 4284    Travel 有N个城市,M条边和H个这个人(PP)必须要去的城市,在每个城市里他都必须要“打工”,打工需要花费Di,可以挣到Ci,每条边有一个花费,现在求PP可不可以从起点1 ...

  3. HDU 4336 容斥原理 || 状压DP

    状压DP :F(S)=Sum*F(S)+p(x1)*F(S^(1<<x1))+p(x2)*F(S^(1<<x2))...+1; F(S)表示取状态为S的牌的期望次数,Sum表示 ...

  4. HDU 3001 Travelling ——状压DP

    [题目分析] 赤裸裸的状压DP. 每个点可以经过两次,问经过所有点的最短路径. 然后写了一发四进制(真是好写) 然后就MLE了. 懒得写hash了. 改成三进制,顺利A掉,时间垫底. [代码] #in ...

  5. HDU - 5117 Fluorescent(状压dp+思维)

    原题链接 题意 有N个灯和M个开关,每个开关控制着一些灯,如果按下某个开关,就会让对应的灯切换状态:问在每个开关按下与否的一共2^m情况下,每种状态下亮灯的个数的立方的和. 思路1.首先注意到N< ...

  6. hdu 4114(状压dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4114 思路:首先是floyd预处理出任意两点之间的最短距离.dp[state1][state2][u] ...

  7. HDU 3091 - Necklace - [状压DP]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3091 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...

  8. HDU 3811 Permutation 状压dp

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3811 Permutation Time Limit: 6000/3000 MS (Java/Othe ...

  9. HDU 5838 (状压DP+容斥)

    Problem Mountain 题目大意 给定一张n*m的地图,由 . 和 X 组成.要求给每个点一个1~n*m的数字(每个点不同),使得编号为X的点小于其周围的点,编号为.的点至少大于一个其周围的 ...

  10. hdu 4628 Pieces 状压dp

    题目链接 枚举所有状态, 1表示这个字符还在原来的串中, 0表示已经取出来了. 代码中j = (j+1)|i的用处是枚举所有包含i状态的状态. #include <iostream> #i ...

随机推荐

  1. 分区数据库oracle自动分区

    首先声明,我是一个菜鸟.一下文章中出现技术误导情况盖不负责 oralce在linux下主动备份并删除5天前备份 导出脚本: export ORACLE_BASE=/opt/oracle         ...

  2. 使用 IDEA 创建 Maven Web 项目 (异常)- Disconnected from the target VM, address: '127.0.0.1:59770', transport: 'socket'

    运行环境: JDK 版本:1.8 Maven 版本:apache-maven-3.3.3 IDEA 版本:14 maven-jetty-plugin 配置: <plugin> <gr ...

  3. Semaphore初探

    示例一: package com.smbea.demo.semaphore; import java.util.concurrent.ExecutorService; import java.util ...

  4. windows系统下快捷命令

    mstsc 远程计算机 regedit 注册表

  5. JavaScript编写连连看

    这几天写题比较少,做了一下网页设计的期末大作业.搞了个连连看,核心代码和hdu 1175那个题目一样. 越来越觉得学ACM是十分有用的,软件的核心是数据结构和算法,学会了DFS,连连看就水到渠成了. ...

  6. 【转】深入理解javascript原型和闭包(完结)

    直接传送门-------------------->>>>>>>> 深入理解javascript原型和闭包(完结)

  7. shell 分支/循环

    ==)); then patern="Update" else patern="Read" fi in "-h") ] then helpI ...

  8. JS 常用功能收集

    JS 常用效果收集 1. 回到顶部>>    爱词霸

  9. ToDictionary用法

    ToDictionary其实可以简单化,可以传两个lambada表达式,第一个是Key,第二个就是Value. ToDictionary( key => key.Attribute(" ...

  10. java 包 修饰符 权限详解

    作用域   当前类    同package   子孙类 其他package  public √   √  √ √  protected √ √ √ ×  friendly(default) √ √ × ...