charge-station
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 1 Accepted Submission(s) : 1
Font: Times New Roman | Verdana | Georgia
Font Size: ← →
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.
分析:给你N个点,然后建立加油站,怎么建立加油站可以使这N个点都可以连一块(就是一个点可以到达任何一个点)
然后保证花的钱最小,按照给的顺序分别是1 2 3 4----N,每个建立的钱就是2的(i-1)次方。
最先开始BFS一遍,看看整个图是否都可以到达,因为有距离限制,然后从最后一个点枚举判断是否可以减去,最后从后边往前边输出,刚开始的0就不要了。
#include<stdio.h>
#include<iostream>
#include <algorithm>
#include<string.h>
#include<vector>
#include<math.h>
#include<queue>
#include<set>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
int KGCD(int a,int b){if(a==0)return b;if(b==0)return a;if(~a&1){ if(b&1) return KGCD(a>>1,b);else return KGCD(a>>1,b>>1) <<1; } if(~b & 1) return KGCD(a, b>>1); if(a > b) return KGCD((a-b)>>1, b);return KGCD((b-a)>>1, a);}
int LCM(int a,int b){ return a/KGCD(a,b)*b; }
int dir[5][2]={0,1,0,-1,1,0,-1,0};
using namespace std;
int a[200][2];
int map[200][200];
int map1[200];
int m,n;
int bfs()
{
queue<int>q;
int dis[200];
int vis[200];
for(int i=0;i<n;i++)
{
if(map1[i]) //默认距离 刚开始如果点可以加油的话就判断下到达这个点最短需要多少
dis[i]=0;
else
dis[i]=INF;
}
q.push(0);//将第一个放进去 每次都是第1个点开始起步,而且第一个点必须加油
memset(vis,0,sizeof(vis));
vis[0]=1;
while(!q.empty())
{
int temp=q.front();
q.pop();
for(int i=0;i<n;i++)
{
if(!vis[i] && map[temp][i]<=m)//可以到达
{
dis[i]=min(dis[i],dis[temp]+map[temp][i]);//刷新下可以到i点的所有点中最短的那个距离
if(map1[i])//当前这个点可以作为加油站
{
q.push(i);
vis[i]=1;
}
// printf("%d %d %d \n",temp,i,dis[i]);
}
}
}
for(int i=0;i<n;i++)
{
if(map1[i] && !vis[i])//这个点有 但是没能到达
return 0;
if(!map1[i] && dis[i]*2>m)//这个地方不能加油,但是加完油回不来了
return 0;
}
return 1;
}
void find()
{
for(int i=n-1;i>0;i--)//开始枚举 判断是否可以去掉
{
map1[i]=0;
if(!bfs())
map1[i]=1;
}
int temp=n-1;
while(!map1[temp])//去掉那些多余的0
temp--;
for(int i=temp;i>=0;i--)
printf("%d",map1[i]);
printf("\n");
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=0;i<n;i++)
scanf("%d%d",&a[i][0],&a[i][1]);
for(int i=0;i<n;i++)//计算距离 距离上取整
{
for(int j=i+1;j<n;j++)
{
double temp=(double)sqrt((a[i][0]-a[j][0])*(a[i][0]-a[j][0])+(a[i][1]-a[j][1])*(a[i][1]-a[j][1]));
if(temp>int(temp))
map[i][j]=map[j][i]=(int)temp+1;
else
map[i][j]=map[j][i]=(int)temp;
}
}
memset(map1,0,sizeof(map1));
for(int i=0;i<n;i++)
map1[i]=1;
if(!bfs())//先跑一遍 看符合题意不 是否可以全部到达
{
printf("-1\n");
continue;
}
else
find();
}
return 0;
}
刚开始卡到了距离问题,怎么求任意两个点的最短,因为给的是随机的,不是按照顺序的大小,后来考虑先将所有可以设为加油点的所有点距离都是0,然后判断第一个点可以到的所有点,然后放到栈里面,这个时候这些点的距离还是0。
然后发现只有到那种去掉加油站的点的距离才是每两个点的距离,否则其余都是0。
总之就是只有是已经可以去掉的点,才可以刷出来最短距离,否则都是0,因为当作加油站点的没办法判断,因为在刚开始的BFS已经判断了所有的点可以在油用完之前到达。所以只有去掉某个点的时候,才会刷新出来到这个点的最短距离。