码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-11-20 21:52:28      阅读:229      评论:0      收藏:0      [点我收藏+]

标签:

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为不建立

 1 #include<stdio.h>
 2 #include<iostream>
 3 #include<string.h>
 4 #include<queue>
 5 #include<math.h>
 6 #include<algorithm>
 7 using namespace std;
 8 const int maxn=150;
 9 const int inf=999999999;
10 int map[maxn][maxn];
11 int n,d;
12 double x[maxn],y[maxn];
13 int dis[maxn];
14 bool vis[maxn];
15 
16 int ans[maxn];
17 
18 int check(){
19       memset(vis,false,sizeof(vis));
20       for(int i=1;i<=n;i++){
21           if(ans[i])
22               dis[i]=0;
23           else
24               dis[i]=inf;
25       }
26       queue<int>que;
27     que.push(1);
28     vis[1]=true;
29     while(!que.empty()){
30        int u=que.front();
31           que.pop();
32           for(int i=1;i<=n;i++){
33               if(!vis[i]&&map[u][i]<=d){
34                    dis[i]=min(dis[i],dis[u]+map[u][i]);
35                    if(ans[i]){
36                        vis[i]=true;
37                        que.push(i);
38                    }
39               }
40           }          
41     }
42     for(int i=1;i<=n;i++){
43        if(ans[i]==1&&!vis[i])
44            return false;
45        else if(ans[i]==0&&dis[i]*2>d)
46            return false;
47     }
48   return true;
49        
50 }
51 
52 int main(){
53        while(scanf("%d%d",&n,&d)!=EOF){
54              memset(map,0,sizeof(map));
55              for(int i=1;i<=n;i++){
56                  scanf("%lf%lf",&x[i],&y[i]);
57              }
58              for(int i=1;i<=n;i++){
59                 for(int j=1;j<=n;j++){
60                     map[i][j]=ceil(sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j])));
61                 }
62              }
63              for(int i=1;i<=n;i++){
64                 ans[i]=1;
65              }
66               if(!check()){
67                  printf("-1\n");
68                 continue; 
69               }
70 
71               for(int i=n;i>=1;i--){
72                   ans[i]=0;
73                   if(!check())
74                       ans[i]=1;
75               }
76             int i;
77         for( i=n;i>=1;i--){
78             if(ans[i]==1)
79                 break;
80         }
81         for(int j=i;j>=1;j--)
82             printf("%d",ans[j]);
83         printf("\n");
84        }
85        return 0;
86 }

 

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

标签:

原文地址:http://www.cnblogs.com/13224ACMer/p/4982339.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!