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. Linux内核 hlist_head/hlist_node结构解析

    内核中的定义: struct hlist_head {    struct hlist_node *first;}; struct hlist_node {    struct hlist_node ...

  2. linux下测试宽带速度

    speedtest-cli是一个用Python编写的轻量级Linux命令行工具,在Python2.4至3.4版本下均可运行.它基于Speedtest.net的基础架构来测量网络的上/下行速率.安装sp ...

  3. mybatis使用@param后掉的坑

    项目里面的一个分页拦截器内某段代码需要判断传入的参数是否属于摸个特定的类,如果不是就直接执行下面的流程,坑爹的@param,在DAO里传入的参数前面加上了这个注解,mabaits内部将传入的参数变成了 ...

  4. Python自学笔记——Matplotlib风羽自定义

    [前言]对于气象专业的小学生来说,风场是预报重要的参考数据,我们所知的风羽有四种:短线代表风速2m/s,长线代表风速4m/s,空心三角代表风速20m/s,实心三角代表风速50m/s.而matplotl ...

  5. [ios]iphone 获取UIWebView内Html方法

    原文:http://blog.csdn.net/diyagoanyhacker/article/details/6564897 获取所有html:NSString *lJs = @"docu ...

  6. php钩子程序设计

      序   作为程序员,设计出优雅而完美的系统,永远是让我们非常兴奋的事情.高手不在于你会多少语言,而在于你有多高的思想.   在设计中,怎么体现自身价值,那就是要比别人多想几步.   讲钩子程序,起 ...

  7. 蓝桥杯 C语言 入门训练 序列求和

    问题描述 求1+2+3+...+n的值. 输入格式 输入包括一个整数n. 输出格式 输出一行,包括一个整数,表示1+2+3+...+n的值. 样例输入 4 样例输出 10 样例输入 100 说明:有一 ...

  8. 前端MVC学习笔记(四)——NodeJS+MongoDB+AngularJS+Bootstrap书店示例

    这章的目的是为了把前面所学习的内容整合一下,这个示例完成一个简单图书管理模块,因为中间需要使用到Bootstrap这里先介绍Bootstrap. 示例名称:天狗书店 功能:完成前后端分离的图书管理功能 ...

  9. sharepoint 2010版本 图文安装

    软件程序:Microsoft Perject Server2010 Microsoft Share Point2010(中文版) 操作系统:windows server 2008 sp1  64位 数 ...

  10. Josephus问题的不同实现方法与总结

    /************************************************************************/ /* Josephus问题--数组实现 */ /* ...