HDU 4435 charge-station (Greedy + BFS)

Source: Internet
Author: User
Charge-Station

Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)

Total submission (s): 616 accepted submission (s): 309

Problem descriptionthere 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 shoshould 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.

Inputthere 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)

Outputfor 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 30 00 30 13 20 00 30 13 10 00 30 116 2330 4037 5249 4952 6431 6252 3342 4152 4157 5862 4242 5727 6843 6758 4858 2737 69
 

Sample output

11111-110111011HintIn 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. 
 

Source2012 Asia Tianjin Regional Contest
In N cities, a person wants to start from City 1 and access n cities at a time before returning to City 1.
His Transportation Tool and car can only walk d meters after the fuel is added.
Gas stations can be built in various cities, and the construction cost of cost is 2 ^ (I-1)
Ask him the minimum cost of building a gas station to allow him to access all cities and return to, in a 2-digit format.

Idea: the cost of building a gas station is 2 ^ (I-1), 1 to the I-1 city of the gas station construction cost is also less than the cost of building a gas station in the first city. Therefore, if you want to be greedy, try to avoid creating gas stations with large numbers. Solution: create gas stations in all cities, and delete them one by one from the largest to the smallest. Because one point can be used multiple times, each construction scheme only needs to ensure that the gas station is connected, and the distance between the other points and the gas station is OK <D/2. (Think about why)
Code:

#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <queue>#define maxn 150using namespace std;int n,m,d,ans,flag,xx;bool oil[maxn],vis[maxn];int dist[maxn][maxn],mind[maxn];struct Node{    double x,y;}pp[maxn];queue<int>q;double caldist(int k1,int k2){    double x,y;    x=pp[k1].x-pp[k2].x;    y=pp[k1].y-pp[k2].y;    return sqrt(x*x+y*y);}void presolve(){    int i,j;    for(i=1;i<=n;i++)    {        for(j=i+1;j<=n;j++)        {            dist[i][j]=dist[j][i]=ceil(caldist(i,j));        }    }}bool bfs(){    int i,j,nx;    for(i=1;i<=n;i++)    {        mind[i]=1000000000;    }    memset(vis,0,sizeof(vis));    while(!q.empty()) q.pop();    vis[1]=1;    q.push(1);    while(!q.empty())    {        nx=q.front();        q.pop();        for(i=1;i<=n;i++)        {            if(vis[i]) continue ;            if(oil[i])            {                if(dist[nx][i]<=d)                {                    vis[i]=1;                    q.push(i);                }            }            else            {                if(mind[i]>dist[i][nx]) mind[i]=dist[i][nx];            }        }    }    for(i=1;i<=n;i++)    {        if(oil[i]&&!vis[i]) return false ;        if(!oil[i]&&2*mind[i]>d) return false ;    }    return true ;}bool solve(){    int i,j;    memset(oil,1,sizeof(oil));    if(!bfs()) return false ;    for(i=n;i>1;i--)    {        oil[i]=0;        if(!bfs()) oil[i]=1;    }    return true ;}int main(){    int i,j;    while(~scanf("%d%d",&n,&d))    {        for(i=1;i<=n;i++)        {            scanf("%lf%lf",&pp[i].x,&pp[i].y);        }        presolve();        if(!solve())        {            printf("-1\n");            continue ;        }        i=n;        while(!oil[i]) i--;        for(;i>=1;i--)        {            printf("%d",oil[i]);        }        printf("\n");    }    return 0;}
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.