hdu 4435 charge-station ( 贪心+bfs )

来源:互联网 发布:大数据平台软件有哪些 编辑:程序博客网 时间:2024/05/16 07:16

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 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 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-110111011
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
2012 Asia Tianjin Regional Contest


题意:
n个城市,一个人想从城市1出发,一次访问n个城市再回到城市1。
他的交通工具车在加过油后只能走d米。
可以在各个城市建造加油站,建造费用cost为2^(i-1)
问能使他访问所有城市且回到1点所需要建造加油站的最小花费,以2进制形式输出。

思路:
建造加油站的费用为2^(i-1),1到i-1城市的都建加油站的花费也比只建第i个城市的加油站花费小。所以想到贪心,尽量使编号大的不建加油站。处理方法为:先将所有的城市都建加油站,然后从大到小一个一个删除。
因为一个点可以走多次,所以每一个建造方案只需要保证加油站都连通,其他点到加油站的距离<d/2就ok了。(想一想,为什么)

代码:
#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;}


 
原创粉丝点击