【贪心】charge-station

原创 2013年12月04日 11:16:29

charge-station

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 876    Accepted Submission(s): 459


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.
 

Source
 

Recommend
zhoujiaqi2010   |   We have carefully selected several similar problems for you:  4812 4811 4810 4809 4808 
 
 
难以想象居然是枚举每个点建或不建。如果不加入贪心,则外层循环就是2^n,不可承受。
贪心非常强力。观察代价是2^i,也就是说标号小的所有点的代价和小于标号大的代价,2^1+2^2+2^3+...2^(i-1)<2^i。贪心之后的枚举外外层循环是n。
 
因此我们先假设建所有的点。必然是可行解。
然后按标号从大到小删。检查是否依然连通,如果不连通则能不删。
 
首先我们需要判断从起点是否能走到所有已建好的加油站,包括从起点一步走到,和从一个加油站到另一个加油站。
然后判断是否能从最近的加油站到每个点往返一次,(根据三角形不等式和贪心加油站的油,这样做最优)。
 
如果都能,则可以删除该点。
 
#include <cstdio>
#include <cstring>
#include <cmath>

int que[2000000];
int dist[140];
bool vis[140];
bool charge[140];
int x[140];
int y[140];
int n;
int D;

int Dist(int a,int b)
{
    return ceil(sqrt(double(((x[a]-x[b])*(x[a]-x[b]) + (y[a]-y[b])*(y[a]-y[b])))));
}

int min(int a,int b)
{
    return a<b?a:b;
}

bool bfs()
{
    int l = 0;
    int r = 0;

    for (int i=1;i<=n;i++)
    {
        vis[i] = false;
        if (charge[i]) dist[i] = 0;
        else dist[i] = 0x3f3f3f3f;
    }
    r ++;
    que[r] = 1;
    vis[1] = true;

    while (l < r)
    {
        l ++;
        int u = que[l];

        for (int v=1;v<=n;v++)
        {
            int d = Dist(u,v);
            if (!vis[v] && d<=D)
            {
                dist[v] = min(dist[v],dist[u]+d);
                if (charge[v])
                {
                    vis[v] = true;
                    r ++;
                    que[r] = v;
                }
            }
        }
    }

    for (int i=1;i<=n;i++)
    {
        if (charge[i] && !vis[i]) return false;
        if (!charge[i] && dist[i]*2>D) return false;
    }
    return true;
}

int main()
{
    while (scanf("%d%d",&n,&D)==2)
    {
        for (int i=1;i<=n;i++)
            charge[i] = true;
        for (int i=1;i<=n;i++)
        {
            scanf("%d%d",x+i,y+i);
        }
        if (!bfs()) {printf("-1\n");continue;}
        for (int i=n;i>1;i--)
        {
            charge[i] = false;
            if (!bfs()) charge[i] = true;
        }

        int i = n;
        while (i>1 && !charge[i]) i--;
        while (i > 0)
        {
            printf("%d",int(charge[i]));
            i --;
        }
        printf("\n");
    }

    return 0;
}

 

charge-station HDU - 4435 (dfs+贪心)

题目‘点击打开链接 ’是一个修加油站的题目; 要求能够能够到达每个城市,并且能够回到初始点,车走路的耗油距离是有限的 ,必须在油用完之前加油, 如果不能到就输出-1,否则输出建加油站的钱(以二进...
  • sinat_36215255
  • sinat_36215255
  • 2017年07月11日 15:02
  • 89

HDOJ4435 charge-station[贪心+并查集]

有n个城市,国王要从1号城市游遍全国再回到原点。可是他的车一次最多只能走D米,所以需要在一些城市建加油站。在第i个城市建加油站的费用是2^(i-1),1号城市必须建,问最少的费用是多少,用二进制形式输...
  • cds225255
  • cds225255
  • 2014年12月03日 17:22
  • 211

Hdu 4435 charge-station 贪心

题意:一辆汽车从起点开始经过所有的城市再回到起点,汽车加满油最多能行驶D米,问应该在哪个城市建立加油站使得汽车能满足上面的要求,在i城市建立一个加油站花费2^i-1 问怎么样建使得花费最小 思路:由于...
  • acm18810549519
  • acm18810549519
  • 2013年10月05日 16:51
  • 487

Hdu 4435 charge-station(BFS+贪心)

题目地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=4435 思路:编号大的点应尽量避免建立加油站(2^0+2^1+2^2+......+2^id...
  • wang2147483647
  • wang2147483647
  • 2016年08月19日 22:06
  • 451

hdu 4435 charge-station  (贪心+图论)

charge-station Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota...
  • u011262722
  • u011262722
  • 2013年08月18日 21:47
  • 1088

hdu 4435 charge-station ( 贪心+bfs )

charge-station Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others...
  • u010228612
  • u010228612
  • 2013年08月19日 10:49
  • 787

2012 Asia Tianjin Regional Contest - charge-station 贪心

注意到对于如果要在第i个城市建oil station,那么他的费用一定会比前面1~i-1个城市都建oil station的费用高。这就可以用来贪心,如果前i-1个城市全部建oil station就已经...
  • GEOTCBRL
  • GEOTCBRL
  • 2015年11月11日 21:00
  • 836

hdu4435 charge-station

charge-station Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) To...
  • u010422038
  • u010422038
  • 2013年08月19日 09:08
  • 663

HDU 4435 charge-station

题意:给出N个点及其坐标,一个人从第一个点开始,要
  • u012139398
  • u012139398
  • 2014年09月12日 18:21
  • 415

贪心 任务调度问题

一个单位时间任务是恰好需要一个单位时间完成的任务。给定一个单位时间任务的有限集S。关于S 的一个时间表用于描述S 中单位时间任务的执行次序。时间表中第1 个任务从时间0 开始执行直至时间1 结束,第2...
  • lvbcy
  • lvbcy
  • 2016年07月30日 13:08
  • 620
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:【贪心】charge-station
举报原因:
原因补充:

(最多只允许输入30个字)