Charge-station HDU - 4435 (greedy)

charge-station HDU - 4435

That Italy: a plane with a \ (n-\) points, numbered \ (. 1-n-\) . The first \ (I \) takes stations is built point \ (I-2. 1 ^ {} \) . After fill up the car can go away for the \ (d \) . Now from \ (1 \) o'clock repeatable through all the points, ask minimum cost to build the gas station.

Solution: Since the first \ (n \) spend points to build gas stations is greater than the sum of all the previous points spent. Can be assumed that the initial gas stations are built, from the greedy \ (n-\) points to the first \ (1 \) points to consider removing stations that point, it is determined whether the Unicom FIG. If you do not link the gas station point is reserved.

Code:

#include <bits/stdc++.h>
#define fopi freopen("in.txt", "r", stdin)
#define fopo freopen("out.txt", "w", stdout)
using namespace std;
typedef long long LL;
const int maxn = 130 + 100;
const int inf = 0x3f3f3f3f;
const double eps = 1e-6;

int n, d, vis[maxn], ans[maxn], flag[maxn];
int x[maxn], y[maxn];

int dist(int a, int b) {
    return (int)ceil(hypot(x[b]-x[a], y[b]-y[a]) - eps);
}

void dfs(int x) {
    vis[x] = 1;
    for (int i = 1; i <= n; i++) if (!vis[i]){
        int l = dist(x, i);
        if (ans[x] && ans[i] && l <= d) dfs(i);
        else if ((ans[x] || ans[i]) && l*2 <= d) dfs(i);
    }
}

bool check() {
    memset(vis, 0, sizeof(vis));
    dfs(1);
    for (int i = 1; i <= n; i++)
        if (!vis[i]) return false;
    return true;
}

int main() {
    //fopi;

    while(~scanf("%d%d", &n, &d)) {
        for (int i = 1; i <= n; i++)
            scanf("%d%d", &x[i], &y[i]), ans[i] = 1;

        if (!check()) {
            printf("-1\n");
            continue;
        }

        for (int i = n; i >= 2; i--) {
            ans[i] = 0;
            if (!check()) ans[i] = 1;
        }

        for (int i = n, cnt = 0; i >= 1; i--)
            if (cnt = (ans[i] ? cnt+1:cnt)) printf("%d", ans[i]);
        puts("");
    }
}

Guess you like

Origin www.cnblogs.com/ruthank/p/11365942.html