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);
            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
");
            continue;
        }
        for (int i = n-1; i >= 1; --i) {
            ans[i] = false;
            //printf("%d:%d
", 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("
");
    }
    return 0;
}
原文地址:https://www.cnblogs.com/wenruo/p/5820593.html