zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    把旧表中数据加入到新表中
    mysql字段-创建时间与更新时间
    springboot-maven依赖源
    刚刚下载的IDEA打不开
    matplotlib-实战01
    实战1-数据清理
    python函数(三)
    交换机配置DHCP中继
    python函数(二)
    用事实说话
  • 原文地址:https://www.cnblogs.com/wenruo/p/5820593.html
Copyright © 2011-2022 走看看