您的位置:首页 > 其它

hunnu 11101 fix 状态压缩的DP

2012-04-16 00:03 134 查看
the problem requires that to calculate a minimum length of verge which is needed in order to fix all the point unfixed to the plain with fixed points.

状态压缩的dp,每个状态,枚举每一个可以添加活点的位置,每次的未固定的点,枚举可以连接的以固定的点

#include<stdio.h>
#include<math.h>
#define N 18
#define INF 1e100
int moveL[N+1];

void cal_move()
{
int i;

}
double min(double a,double b)
{
return a<b?a:b;
}
struct node
{
int x,y;
void input(){
scanf("%d%d",&x,&y);
}
}point
;
double dist

;

double dp[1<<N];
int main()
{
// cal_move();
int n,i,j,k,c,ini;
for(i=0;i<=N;++i)
moveL[i]=1<<i;
while(scanf("%d",&n)==1&&n)
{
ini = 0;
for(i = 0;i<n;++i)
{
point[i].input();
scanf("%d",&c);
if(c)
ini|=moveL[i];
}
for(i=0;i<n;++i)
for(j=0;j<=i;++j)
dist[i][j]=dist[j][i]=
sqrt((point[i].x-point[j].x+0.0)*(point[i].x-point[j].x+0.0)+(point[i].y-point[j].y+0.0)*(point[i].y-point[j].y+0.0));
for(i=0;i<moveL
;++i)
dp[i]=INF;
dp[ini]=0.0;
double mina,minb;
for(i=ini;i<moveL
;++i)
/**if dp[i]==INF it's not necessary to update its next staus
however on the hdu_3362 ,because of longer time,it can be AC without this judgement,
hunnu not
*/
if(dp[i]<INF)
for(j=0;j<n;++j)
if((i>>j&1)==0)
{
mina=minb=INF;
for(k=0;k<n;++k)
{
if(i>>k&1)
{
if(dist[j][k]<mina)
{
minb=mina;
mina=dist[j][k];
}
else if(dist[j][k]<minb)
minb=dist[j][k];
}
}
if(dp[i|moveL[j]]>dp[i]+mina+minb)
dp[i|moveL[j]]=dp[i]+mina+minb;
}
if(dp[moveL
-1]==INF)
printf("No Solution\n");
else
printf("%.6lf\n",dp[moveL
-1]);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: