charge-station
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 610 Accepted Submission(s): 306
Problem 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.
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)
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.
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.
Source
这题,主要是2的幂,要想到用贪心,首先选择,较小的点建就可以了,但是,这里,要注意一点,我们可以先全部建,再一个个去,如果去掉的这个,不能满足条件,就要加上,否则去掉,这一个思想是很重要的,然后,用一个bfs 或者dfs都可以解决问题!
#include <iostream> #include <stdio.h> #include <math.h> #include <string.h> using namespace std; struct node { double x,y;int flag; }; struct qtree{ int now; }; qtree queue[1000000]; node p[130]; int dis[130][130],visit[150],n,d,station; bool bfs() { station=1; memset(visit,0,sizeof(visit)); int s=0,e=0,i; queue[s].now=1,visit[1]=1; qtree qfront ,temp; while(s<=e) { qfront=queue[s]; for(i=1;i<=n;i++) { temp=qfront; if(!visit[i]&&p[i].flag&&dis[temp.now][i]<=d) { visit[i]=1; temp.now=i; queue[++e]=temp; station++; } } s++; } s=0; while(s<=e) { qfront=queue[s]; for(i=1;i<=n;i++) { temp=qfront; if(!visit[i]&&!p[i].flag&&dis[temp.now][i]<=d/2.0) { visit[i]=1; station++; } } s++; } if(station!=n) return false; else return true; } int main() { int i,j; while(scanf("%d%d",&n,&d)!=EOF) { for(i=1;i<=n;i++) { scanf("%lf%lf",&p[i].x,&p[i].y); p[i].flag=1; } for(i=1;i<=n;i++) for(j=1;j<=n;j++) dis[i][j]=ceil(sqrt((p[i].x-p[j].x)*(p[i].x-p[j].x)+(p[i].y-p[j].y)*(p[i].y-p[j].y))); if(!bfs()){printf("-1 ");continue;} for(i=n;i>1;i--) { p[i].flag=0; if(bfs()) p[i].flag=0; else p[i].flag=1; } bool flag=false; for(i=n;i>=1;i--) { if(p[i].flag) { putchar('1'),flag=true; } else { if(flag) putchar('0'); } } printf(" "); } return 0; }