小明系列故事――女友的考验

Time limit:200ms Memory limit:32768kB

链接

http://acm.hdu.edu.cn/showproblem.php?pid=4511

题意

现在给出n个点和他们的坐标,从1号点出发到达n点,每次只能到达序号更远的点,但是现在有m条路径是不允许走的,问在这些条件下从1号点出发到达n号点的最短距离是多少?

思路

题解

代码

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;

const int maxn = 505;
const double inf = 1e20;
int kind;

struct poi
{
    double x, y;
} p[maxn];

int root, cnt;
int nxt[maxn][55];
int fail[maxn];
int mrk[maxn];

int newnode ()
{
    ++cnt;
    for (int i = 1; i < kind; ++i) {
        nxt[cnt][i] = -1;
    }
    mrk[cnt] = 0;
    return cnt;
}

void ac_init()
{
    cnt = -1;
    root = newnode();
}

void wd_insert (int str[], int len)
{
    int now = root;
    for (int i = 0; i < len; ++i) {
        if (nxt[now][str[i]] == -1) {
            nxt[now][str[i]] = newnode();
        }
        now = nxt[now][str[i]];
    }
    mrk[now] = 1;
}

void build ()
{
    queue<int> q;
    fail[root] = root;
    for (int i = 1; i < kind; ++i) {
        if (nxt[root][i] == -1) {
            nxt[root][i] = root;
        }
        else {
            fail[nxt[root][i]] = root;
            q.push(nxt[root][i]);
        }
    }
    while (!q.empty()) {
        int now = q.front();
        q.pop();
        mrk[now] |= mrk[fail[now]];
        for (int i = 1; i < kind; ++i) {
            if (nxt[now][i] == -1) {
                nxt[now][i] = nxt[fail[now]][i];
            }
            else {
                fail[nxt[now][i]] = nxt[fail[now]][i];
                q.push(nxt[now][i]);
            }
        }
    }
}

int n, m;
int buf[maxn];

double dp[55][maxn];

double dis (int i, int j)
{
    double x0 = p[i].x, y0 = p[i].y;
    double x1 = p[j].x, y1 = p[j].y;
    return sqrt((x0-x1)*(x0-x1)+(y0-y1)*(y0-y1));
}

int main ()
{
    while (scanf("%d %d", &n, &m) != EOF && n+m) {
        kind = n+1;
        ac_init();
        for (int i = 1; i <= n; ++i) {
            scanf("%lf %lf", &p[i].x, &p[i].y);
        }
        for (int i = 1; i <= m; ++i) {
            int len;
            scanf("%d", &len);
            for (int j = 0; j < len; ++j) {
                scanf("%d", &buf[j]);
            }
            wd_insert(buf, len);
        }
        build();
        for (int i = 1; i <= n; ++i) {
            for (int j = 0; j <= cnt; ++j) {
                dp[i][j] = inf;
            }
        }
        dp[1][nxt[root][1]] = 0;
        for (int i = 1; i <= n; ++i) {
            for (int j = 0; j <= cnt; ++j) {
                if (mrk[j] || dp[i][j] >= inf) {
                    continue;
                }
                for (int k = i+1; k < kind; ++k) {
                    if (mrk[nxt[j][k]]) {
                        continue;
                    }
                    dp[k][nxt[j][k]] = min(dp[k][nxt[j][k]], dp[i][j]+dis(i, k));
                }
            }
        }
        double res = inf;
        for (int i = 0; i <= cnt; ++i) {
            res = min(res, dp[n][i]);
        }
        if (res == inf) {
            cout << "Can not be reached!" << '\n';
        }
        else {
            printf("%.2f\n", res);
        }
    }
    return 0;
}

发表评论