E - charge-station

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Appoint description:

Description

There are n cities in M^3's empire. M^3 owns a palace and a car and the palace resides in city 1. One day, she wants to travel around all the cities from her palace and finally back to her home. However, her car has limited energy and can only travel by no more than D meters. Before it was run out of energy, it should be charged in some oil station. Under M^3's despotic power, the judge is forced to build several oil stations in some of the cities. The judge must build an oil station in city 1 and building other oil stations is up to his choice as long as M^3 can successfully travel around all the cities.
Building an oil station in city i will cost 2 i-1 MMMB. Please help the judge calculate out the minimum cost to build the oil stations in order to fulfill M^3's will.
 

Input

There are several test cases (no more than 50), each case begin with
two integer N, D (the number of cities and the maximum distance the car
can run after charged, 0 < N ≤ 128).

Then follows N lines and line i will contain two numbers x, y(0 ≤ x, y ≤ 1000), indicating the coordinate of city i.

The distance between city i and city j will be ceil(sqrt((xi - xj)
2 + (yi - yj)
2)). (ceil means rounding the number up, e.g. ceil(4.1) = 5)
 

Output

For each case, output the minimum cost to build the oil stations in the binary form without leading zeros.

If it's impossible to visit all the cities even after all oil stations are build, output -1 instead.
 

Sample Input

3 3
0 0
0 3
0 1

3 2
0 0
0 3
0 1

3 1
0 0
0 3
0 1

16 23
30 40
37 52
49 49
52 64
31 62
52 33
42 41
52 41
57 58
62 42
42 57
27 68
43 67
58 48
58 27
37 69

 

Sample Output

11
111
-1
10111011

Hint

 In case 1, the judge should select (0, 0) and (0, 3) as the oil station which result in the visiting route: 1->3->2->3->1. And the cost is 2^(1-1) + 2^(2-1) = 3.
大概提议就是给出n和d,下面分别给出n个点的坐标,汽车一次加油可以行使长度为d的距离。但是中途可以加油,问在哪些地方加油可以使得汽车行使完整个来回路程,并且要求建立加油站花费的金额最小,
输出就是二进制判断,1为建立,0为不建立
 #include<stdio.h>
#include<iostream>
#include<string.h>
#include<queue>
#include<math.h>
#include<algorithm>
using namespace std;
const int maxn=;
const int inf=;
int map[maxn][maxn];
int n,d;
double x[maxn],y[maxn];
int dis[maxn];
bool vis[maxn]; int ans[maxn]; int check(){
memset(vis,false,sizeof(vis));
for(int i=;i<=n;i++){
if(ans[i])
dis[i]=;
else
dis[i]=inf;
}
queue<int>que;
que.push();
vis[]=true;
while(!que.empty()){
int u=que.front();
que.pop();
for(int i=;i<=n;i++){
if(!vis[i]&&map[u][i]<=d){
dis[i]=min(dis[i],dis[u]+map[u][i]);
if(ans[i]){
vis[i]=true;
que.push(i);
}
}
}
}
for(int i=;i<=n;i++){
if(ans[i]==&&!vis[i])
return false;
else if(ans[i]==&&dis[i]*>d)
return false;
}
return true; } int main(){
while(scanf("%d%d",&n,&d)!=EOF){
memset(map,,sizeof(map));
for(int i=;i<=n;i++){
scanf("%lf%lf",&x[i],&y[i]);
}
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
map[i][j]=ceil(sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j])));
}
}
for(int i=;i<=n;i++){
ans[i]=;
}
if(!check()){
printf("-1\n");
continue;
} for(int i=n;i>=;i--){
ans[i]=;
if(!check())
ans[i]=;
}
int i;
for( i=n;i>=;i--){
if(ans[i]==)
break;
}
for(int j=i;j>=;j--)
printf("%d",ans[j]);
printf("\n");
}
return ;
}

HDU 4435 charge-station bfs图论问题的更多相关文章

  1. HDU 4435 charge-station () bfs图论问题

    E - charge-station Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u ...

  2. HDU 1428 漫步校园 (BFS+优先队列+记忆化搜索)

    题目地址:HDU 1428 先用BFS+优先队列求出全部点到机房的最短距离.然后用记忆化搜索去搜. 代码例如以下: #include <iostream> #include <str ...

  3. hdu - 2645 find the nearest station (bfs水)

    http://acm.hdu.edu.cn/showproblem.php?pid=2645 找出每个点到距离最近的车站的距离. 直接bfs就好. #include <cstdio> #i ...

  4. hdu 4435 bfs+贪心

    /* 题意:给定每个点在平面内的坐标,要求选出一些点,在这些点建立加油站,使得总花费最少(1号点必须建立加油站).在i点建立加油站需要花费2^i. 建立加油站要求能使得汽车从1点开始走遍全图所有的点并 ...

  5. hdu 3879 Base Station 最大权闭合图

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3879 A famous mobile communication company is plannin ...

  6. HDU 3879 Base Station

    Base Station Time Limit: 2000ms Memory Limit: 32768KB This problem will be judged on HDU. Original I ...

  7. hdu 2102 A计划-bfs

    Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...

  8. HDU 1072(记忆化BFS)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1072 题目大意:走迷宫.走到装置点重置时间,到达任一点时的时间不能为0,可以走重复路,求出迷宫最短时 ...

  9. HDU 2364 (记忆化BFS搜索)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2364 题目大意:走迷宫.从某个方向进入某点,优先走左或是右.如果左右都走不通,再考虑向前.绝对不能往 ...

随机推荐

  1. android之对话、单复框的使用

    对话框 通过builder来构建一个单选框 package xidian.dy.com.chujia; import android.content.DialogInterface; import a ...

  2. 【Groovy基础系列】 Groovy运算符

    ?运算符 在java中,有时候为了避免出现空指针异常,我们通常需要这样的技巧: if(rs!=null){ rs.next() … … } 在groovy中,可以使用?操作符达到同样的目的: rs?. ...

  3. msf命令全集

    一.msfconsole ?   帮助菜单 back 从当前环境返回 banner   显示一个MSF banner cd   切换目录 color   颜色转换 connect   连接一个主机 e ...

  4. iOS边练边学--iOS中的(ARC下)单粒模式(GCD实现)

    一.ARC中实现单粒模式 在.m 保留一个全局的static的实例 static id _名称; 重写allocWithZone:方法,在这里创建唯一的实例 提供一个类方法让外界访问唯一的实例 实现c ...

  5. Java算法-归并排序

    归并排序采用的是递归来实现,属于“分而治之”,将目标数组从中间一分为二,之后分别对这两个数组进行排序,排序完毕之后再将排好序的两个数组“归并”到一起,归并排序最重要的也就是这个“归并”的过程,归并的过 ...

  6. web前端开发常用的10个高端CSS UI开源框架

    web前端开发常用的10个高端CSS UI开源框架   随着人们对体验的极致追求,web页面设计也面临着新的挑战,不仅需要更人性化的设计理念,还需要设计出更酷炫的页面.作为web前端开发人员,运用开源 ...

  7. 【POJ 2484】A Funny Game

    Description Alice and Bob decide to play a funny game. At the beginning of the game they pick n(1 &l ...

  8. angularjs-$interval使用

    1. 简单使用 var app = angular.module("app",[]); app.controller("AppCtrl", function($ ...

  9. TYVJP1933 绿豆蛙的归宿

    背景 随着新版百度空间的上线,Blog宠物绿豆蛙完成了它的使命,去寻找它新的归宿. 描述 给出一个有向无环图,起点为1终点为N,每条边都有一个长度,并且从起点出发能够到达所有的点,所有的点也都能够到达 ...

  10. pthread_kill

    别被名字吓到,pthread_kill可不是kill,而是向线程发送signal.还记得signal吗,大部分signal的默认动作是终止进程的运行,所以,我们才要用signal()去抓信号并加上处理 ...