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 2i-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 2i-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 10111011HintIn 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个城市,国王要从1号城市游遍全国再回到原点。可是他的车一次最多只能走D米,所以需要在一些城市建加油站。在第i个城市建加油站的费用是2^(i-1),1号城市必须建,问最少的费用是多少,用二进制形式输出结果。任意两个城市都是可直达的,给出所有城市的坐标,城市间距离为两点直线距离向上取整。
注意可以多次到达同一个城市。
直接用一个01数组表示各城市是否建加油站,最后只需最后一个非零位开始输出即可。
开始先假设所有的都建上,判断能否连通。
然后从后往前判断,靠后的城市能不建就不建(想想二进制,10000 > 01111)
判断连通性,用并查集就好了。如果两个城市都建,它们的距离只要小于D就能并到一起,如果一个建一个不建,那需要距离小于D/2才行。
#include<iostream> #include<cstring> #include<cstdio> #include<cmath> #define rep(i,f,t) for(int i = (f),_end = (t); i <= (_end); ++i) #define debug(x) cout<<" debug "<<x<<endl; #define clr(cnt,x) memset(cnt,x,sizeof(cnt)); using namespace std; const int maxn = 130; int n,D; int cnt[maxn]; int p[maxn]; #define x first #define y second typedef pair<double,double> Pair; Pair ct[130]; bool ans[130]; void init(){ rep(i,1,n)p[i] = i; rep(i,1,n)cnt[i] = 1; } int Find(int i){ return i == p[i] ? i : p[i] = Find(p[i]); } void Union(int i,int j){ i = Find(i); j = Find(j); if(i == j)return ; p[j] = i; cnt[i] += cnt[j]; } int dis(int i,int j){ double t1 = pow(ct[i].x-ct[j].x,2); double t2 = pow(ct[i].y-ct[j].y,2); double res = sqrt(t1 + t2); return ceil(res); } bool ok(){ init(); rep(i,1,n){ if(!ans[i])continue; rep(j,1,n){ if(Find(i) == Find(j))continue; int ds = dis(i,j); if(ans[j]){ if(ds <= D)Union(i,j); }else{ if(ds*2 <= D)Union(i,j); } } } int i = Find(1); return cnt[i]==n; } int main(){ while(scanf("%d%d",&n,&D) == 2){ rep(i,1,n){ scanf("%lf%lf",&ct[i].x,&ct[i].y); } clr(ans,1); if(!ok()){ printf("-1 "); continue; } for(int i = n; i > 1; --i){ ans[i] = 0; if(ok())continue; ans[i] = 1; } bool flg = 0; for(int i = n; i > 0; --i){ if(ans[i] && !flg)flg = 1; if(!flg)continue; int an = ans[i]; printf("%d",an); } printf(" "); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。