HDU 4435 charge-station(暴力+判图)

题目大意:给你一些点,他们可以连通,如果距离超过了d那么就要经过加油站,建立加油站的费用为第i个点是2^(i-1)。求费用最小,输出二进制表示的最小费用。

费用和sum最坏等于=2^0+2^1+……+2^(n-1)。所以最高位为0这个数字才会最小,从最高位暴力枚举如果删掉这个点之后图是连通的那么就可以删掉,否则不可以。

求图是否连通的时候可以爆搜求解,也可以并查集。

charge-station

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1053    Accepted Submission(s): 554

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

2012 Asia Tianjin Regional Contest

#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <time.h>
#include <stack>
#include <map>
#include <set>
#define eps 1e-8
///#define LL long long
#define LL __int64
#define INF 0x3f3f3f
#define PI 3.1415926535898
#define mod 1000000007

using namespace std;

const int maxn = 210;

bool vis[maxn];
int mp[maxn][maxn];

struct node
{
    int x, y;
} f[maxn];

int n, d;

double Dis(int a, int b)
{
    return sqrt(((f[a].x-f[b].x)*(f[a].x-f[b].x) + (f[a].y-f[b].y)*(f[a].y-f[b].y))*1.0);
}

bool bfs()
{
    bool flag[maxn];
    int dis[maxn];
    memset(flag, false, sizeof(flag));
    for(int i = 1; i <= n; i++)
    {
        if(vis[i])
        {
            dis[i] = 0;
            continue;
        }
        dis[i] = INF;
    }
    queue<int> que;
    que.push(1);
    flag[1] = true;
    while(!que.empty())
    {
        int x = que.front();
        que.pop();
        for(int i = 1; i <= n; i++)
        {
            if(flag[i] || mp[x][i] > d) continue;
            dis[i] = min(dis[i], dis[x]+mp[x][i]);
            if(vis[i])
            {
                flag[i] = true;
                que.push(i);
            }
        }
    }
    for(int i = 1; i <= n; i++)
    {
        if(vis[i] && !flag[i]) return false;
        if(!vis[i] && 2*dis[i] > d) return false;
    }
    return true;
}

int main()
{
    while(~scanf("%d %d",&n, &d))
    {
        for(int i = 1; i <= n; i++) scanf("%d %d",&f[i].x, &f[i].y);
        for(int i = 1; i <= n; i++)
            for(int j = 1; j <= n; j++)
                mp[i][j] = ceil(Dis(i, j));
        for(int i = 1; i <= n; i++) vis[i] = true;
        if(!bfs())
        {
            cout<<-1<<endl;
            continue;
        }
        for(int i = n; i >= 1; i--)
        {
            vis[i] = false;
            if(!bfs()) vis[i] = true;
        }
        int x = n;
        while(!vis[x]) x--;
        for(int i = x; i >= 1; i--) cout<<vis[i];
        cout<<endl;
    }
    return 0;
}
时间: 2024-11-10 13:11:19

HDU 4435 charge-station(暴力+判图)的相关文章

HDU 4435 charge-station () bfs图论问题

E - charge-station Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 4435 Appoint description: Description There are n cities in M^3's empire. M^3 owns a palace and a car and the palace resides in

hdu 3879 hdu 3917 构造最大权闭合图 俩经典题

hdu3879  base station : 各一个无向图,点的权是负的,边的权是正的.自己建一个子图,使得获利最大. 一看,就感觉按最大密度子图的构想:选了边那么连接的俩端点必需选,于是就以边做点,轻轻松松构造了最大权闭合图.简单题.分分钟搞定. hdu3917 :road  constructions :这题题目看了半天没理解...感觉描述的不好...一个有向图,每条路有响应公司承保,若选了该公司,那么该公司的路必需全部选,还有,该公司的承保的路的下面的一条路对应公司也要选,求最大获利.构

HDU 4499 Cannon (暴力搜索)

题意:在n*m的方格里有t个棋子,问最多能放多少个炮且每个炮不能互相攻击(炮吃炮) 炮吃炮:在同一行或同一列且中间有一颗棋子. #include <stdio.h> #include <iostream> #include <algorithm> #include <string.h> #include <queue> #include <math.h> #define M 50 #define LL long long using

HDU 2147 kiki&#39;s game(博弈图上找规律)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2147 题目大意:给你一个n*m的棋盘,初始位置为(1,m),两人轮流操作,每次只能向下,左,左下这三个方向移动,谁最后无法移动棋子就输掉比赛,问先手是否会获胜. 解题思路:简单题,P/N分析找规律,以(n,m)点为结束点推到起始点,如图: 发现每个田字格的状态都是一样的,因为(n,m)点一定时P态,所以可以得出规律:只有当(m%2==1&&n%2==1)时,先手才会输. 代码: 1 #incl

HDU 4902 线段树||暴力

给定一个序列,两种操作 1:把一段变成x. 2:把一段每个数字,如果他大于x,就变成他和x的gcd,求变换完后,最后的序列. 线段树解法:用lazy标记下即可,优化方法还是很巧妙的, Accepted 4902 515MS 3308K 1941 B C++ #include "stdio.h" #include "string.h" struct node { int l,r,x;// 在叶子节点代表值,树节点代表成端更新的lazy操作. }data[400010]

HDU 4831 Scenic Popularity 暴力模拟

Scenic Popularity Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 340    Accepted Submission(s): 110 Problem Description 临近节日,度度熊们最近计划到室外游玩公园,公园内部包括了很多的旅游景点区和休息区,由于旅游景点很热门,导致景点区和休息区都聚集了很多人.所以度度熊

HDU 4961 Boring Sum 暴力

题意:对于所有的A[I],同时找到左边和右边离它最近且是它的倍数的数相乘最后加起来求和. 解题思路:n*sqrt(n)的算法,开始以为过不了,wa了两发因为lld I64d对拍一个小时发现一个小时前交的代码没错只是没变I64d,..具体思路是枚举每个a[i]的因子,找离它最近的那个更新,如果已经没更新就不用更新了.用两个辅助数组记录最近的就行. 解题代码: 1 // File Name: 1002.cpp 2 // Author: darkdream 3 // Created Time: 201

HDU - 2089 不要62 (暴力或数位DP)

Description 杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer). 杭州交通管理局经常会扩充一些的士车牌照,新近出来一个好消息,以后上牌照,不再含有不吉利的数字了,这样一来,就可以消除个别的士司机和乘客的心理障碍,更安全地服务大众. 不吉利的数字为所有含有4或62的号码.例如: 62315 73418 88914 都属于不吉利号码.但是,61152虽然含有6和2,但不是62连号,所以不属于不吉利数字之列. 你的任务是,对于每次给出的一个牌照区间号,推断出交管局今次又要实际上给多少辆新

HDU 2448 Mining Station on the Sea 费用流

Mining Station on the Sea Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2123    Accepted Submission(s): 642 Problem Description The ocean is a treasure house of resources and the development o