hdu4435-charge-station(搜索+贪心)

题意&题解:

http://www.cnblogs.com/wuminye/p/3245546.html

 

说实话看了题解觉得很简单,但是比赛的时候真的是毫无头绪。

然而印象中做过一道类似的二进制贪心的题目。竟然……没想到……sigh……太弱辣!!!

应该思考题目为什么要给2^i,而不是轻易的认为是为了增加难度= =

笨死了555~

 

//补题
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <iostream>
using namespace std;
typedef long long ll;

const int N = 150;
const int INF = 0x5f5f5f5f;
const int MOD = 1000000007;

int n, d;
int x[N], y[N];
int dis[N][N];
bool ans[N];
bool vis[N];

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

bool ok() {
    memset(vis, false, sizeof vis);
    vis[0] = true;
    queue<int> q;
    q.push(0);
    while(q.size()) {
        int top = q.front(); q.pop();
        for (int i = 0; i < n; ++i) {
            if (vis[i]) continue;
            if (dis[top][i] <= d && ans[i]) {
                vis[i] = true;
                q.push(i);
            } else if (dis[top][i] * 2 <= d) {
                vis[i] = true;
            }
        }
    }
    for (int i = 0; i < n; ++i) if (!vis[i]) return false;
    return true;
}

int main()
{
    //freopen("in.txt", "r", stdin);
    while (~scanf("%d%d", &n, &d)) {
        for (int i = 0; i < n; ++i) {
            scanf("%d%d", x+i, y+i);
        }
        if (n <= 1) {
            printf("%d\n", n);
            continue;
        }
        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                dis[i][j] = get_dis(i, j);
            }
        }
        memset(ans, true, sizeof ans);
        if (!ok()) {
            printf("-1\n");
            continue;
        }
        for (int i = n-1; i >= 1; --i) {
            ans[i] = false;
            //printf("%d:%d\n", i, ok());
            if (!ok()) ans[i] = true;
        }
        bool f = true;
        for (int i = n-1; i >= 0; --i) {
            if (f && !ans[i]) continue;
            if (ans[i]) f = false;
            printf("%d", ans[i]);
        }
        printf("\n");
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/wenruo/p/5820593.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值