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. css中的默认margin

    上班打酱油中,你懂的; body的margin为8px; webkit默认行高18px:height18px; 默认font-size16px p默认margin是16px 0 16px 0; ul和 ...

  2. Java常见异常

    1. runtimeException子类: 1. java.lang.ArrayIndexOutOfBoundsException    数组索引越界异常.当对数组的索引值为负数或大于等于数组大小时 ...

  3. echo 和 cat 的 区别

    tt="1 10 17 10-134-9-154.xml" echo $tt 只是单纯地打印出tt保存的这些变量 cat $tt 则会对tt 中保存的变量文件挨个打印出来

  4. 数组添加:如何往数组的"null"位置插入数据呢?

    数组添加,当已经存在的一个数组时,如何往数组的"null"位置插入数据呢? 分析: 1.循环遍历数组元素,找出null的位置(下标) 2.设置一个变量,接收null位置下标值 3. ...

  5. HD2444The Accomodation of Students(并查集判断二分图+匹配)

    The Accomodation of Students Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  6. 斯坦福大学CS224d基础1:线性代数回顾

    转自 http://blog.csdn.net/han_xiaoyang/article/details/51629242 斯坦福大学CS224d基础1:线性代数知识 作者:Zico Kolter ( ...

  7. nginx try_files命令

    location / { index index.html index.htm index.php l.php; autoindex on; try_files $uri $uri/ /index.p ...

  8. javascript console 函数详解 js开发调试的利器

    Console 是用于显示 JS和 DOM 对象信息的单独窗口.并且向 JS 中注入1个 console 对象,使用该对象 可以输出信息到 Console 窗口中. 使用 alert 不是一样可以显示 ...

  9. 使用注解实现IOC

    在biz业务处理类实现类中 /** * 用户业务类,实现对User功能的业务管理 */ @Service("userBiz") public class UserBiz imple ...

  10. System.gc()与Object.finalize()的区别

    finalize()是由JVM自动调用的,你可以用System.gc(),但JVM不一定会立刻执行,JVM感觉内存空间有限时,才会开始执行finalize(),至于新的对象创建个数和被收集个数不同是因 ...