charge-station
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1288 Accepted Submission(s): 683
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.
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个点,n条无向边,最大载油量D,因为油量有限,所以需要建一些补充油量的站点,在点i处建立油站需要花费:2^(i-1)。一个人从点1出发,最终回到点1,问是否有这样的解,如果有,输出最小花费,否则输出-1.
思路:
1、首先,我们不希望编号大的点建油站,所以我们从n开始向1贪心判断,如果去掉了当前节点还能保证有解,那么这个点就不需要建油站,就这样暴力求得其解,那么如何判断去掉当前点之后能否保证有解呢?我们可以采用Bfs的方式来搞定这个问题,其时间复杂度:O(N^3);
2、Bfs的过程中,我们假设保证当前点是油站点,那么能够到达的点中,如果那个点是油站,那么K-map【u】【v】>=0即可走到点v,如果那个点不是油站,那么K-2*map【u】【v】即可走到点v并且回到当前油站。
3、所以我们保证当前点是油站点就行,那么我们入队的点也都是油站点,然后一直判断下去,贪心求解即可。
Ac代码:
#include<stdio.h>
#include<string.h>
#include<vector>
#include<queue>
#include<math.h>
using namespace std;
int map[150][150];
int x[1500];
int y[1500];
int ans[1500];
int vis[1500];
int n,k,cont;
int Bfs(int tmp)
{
memset(vis,0,sizeof(vis));
vis[1]=1;
queue<int >s;
s.push(1);
while(!s.empty())
{
int u=s.front();
s.pop();
for(int i=1;i<=n;i++)
{
if(vis[i]==0)
{
if(ans[i]==1)
{
if(k-map[u][i]>=0)
{
vis[i]=1;
s.push(i);
}
}
else
{
if(k-2*map[u][i]>=0)
{
vis[i]=1;
//s.push(i);
}
}
}
}
}
for(int i=1;i<=n;i++)
{
if(vis[i]==0)return 0;
}
return 1;
}
void Slove()
{
for(int i=1;i<=n;i++)ans[i]=1;
if(Bfs(0)==0)
{
printf("-1\n");
return ;
}
for(int i=n;i>=2;i--)
{
ans[i]=0;
if(Bfs(i)==0)ans[i]=1;
}
int z=n;
for(z=n;z>=1;z--)
{
if(ans[z]==1)break;
}
for(int i=z;i>=1;i--)
{
printf("%d",ans[i]);
}
printf("\n");
}
int main()
{
while(~scanf("%d%d",&n,&k))
{
for(int i=1;i<=n;i++)
{
scanf("%d%d",&x[i],&y[i]);
}
for(int i=1;i<=n;i++)
{
for(int j=i+1;j<=n;j++)
{
int di=ceil(sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j])));
map[i][j]=di;
map[j][i]=di;
}
}
Slove();
}
}
/*
16 20
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
1001111111
*/