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. C#基础知识系列七(base、this、new、override、abstract、virtual、static)

    前言 本文主要来讲解一下C#中,自己觉得掌握的不怎么样或者用的不多,不太熟悉的关键字,主要包括base.this.new.override.abstract.virtual以及针对static字段和s ...

  2. ios开发怎么获取输入的日期得到星期

    + (NSString*)weekdayStringFromDate:(NSDate*)inputDate { NSArray *weekdays = [NSArray arrayWithObject ...

  3. 并行程序设计模式--Master-Worker模式

    简介 Master-Worker模式是常用的并行设计模式.它的核心思想是,系统有两个进程协议工作:Master进程和Worker进程.Master进程负责接收和分配任务,Worker进程负责处理子任务 ...

  4. Ibatis的类型处理器TypeHandler解析

    Ibatis允许用户像在hibernate中一样定义自己的类型,但是,用户自定义类型需要与数据库中的字段类型进行对应.它的处理方法是允许我们扩展TypeHandler.Ibatis框架在处理该数据类型 ...

  5. HTML5动画实例

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  6. 使用XML序列化器生成XML文件和利用pull解析XML文件

    首先,指定XML格式,我指定的XML格式如下: <?xml version='1.0' encoding='utf-8' standalone='yes' ?> <message&g ...

  7. Oracle修改数据表

    (1)修改数据表中某个字段值为空: update 表明   别名  set 别名.字段名 例子:update JWT_JYWF t set t.jdsbh=''(2)设置数据表中某个字段值和rownu ...

  8. CVE: 2014-6271、CVE: 2014-7169 PATCH方案分析

    目录 . RedHat官方给的PATCH第一套方案 . RedHat官方给的PATCH临时方案 . RedHat官方给的PATCH第二套方案 1. RedHat官方给的PATCH第一套方案 0x1: ...

  9. 加强版DVD管理系统

    这个加强版,只做了新增和查看. 主要是在新增代码那里增加了一些处理: 进入新增操作,一直可以不跳出来,每次新增成功后,问你是否继续,输入y就继续,输入n就不继续 代码如下: import java.u ...

  10. POJ1037A decorative fence(动态规划+排序计数+好题)

    http://poj.org/problem?id=1037 题意:输入木棒的个数n,其中每个木棒长度等于对应的编号,把木棒按照波浪形排序,然后输出第c个; 分析:总数为i跟木棒中第k短的木棒 就等于 ...