码迷,mamicode.com
首页 > 其他好文 > 详细

hdu4511---小明系列故事——女友的考验(AC自动机+dp)

时间:2015-04-20 17:06:34      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:ac自动机   dp   

小明系列故事——女友的考验
Time Limit: 500/200 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 863 Accepted Submission(s): 192

Problem Description
  终于放寒假了,小明要和女朋友一起去看电影。这天,女朋友想给小明一个考验,在小明正准备出发的时候,女朋友告诉他,她在电影院等他,小明过来的路线必须满足给定的规则:
  1、假设小明在的位置是1号点,女朋友在的位置是n号点,则他们之间有n-2个点可以走,小明每次走的时候只能走到比当前所在点编号大的位置;
  2、小明来的时候不能按一定的顺序经过某些地方。比如,如果女朋友告诉小明不能经过1 -> 2 -> 3,那么就要求小明来的时候走过的路径不能包含有1 -> 2 -> 3这部分,但是1 -> 3 或者1 -> 2都是可以的,这样的限制路径可能有多条。
  这让小明非常头痛,现在他把问题交给了你。
  特别说明,如果1 2 3这三个点共线,但是小明是直接从1到3然后再从3继续,那么此种情况是不认为小明经过了2这个点的。
  现在,小明即想走最短的路尽快见到女朋友,又不想打破女朋友的规定,你能帮助小明解决这个问题吗?

Input
  输入包含多组样例,每组样例首先包含两个整数n和m,其中n代表有n个点,小明在1号点,女朋友在n号点,m代表小明的女朋友有m个要求;
  接下来n行每行输入2个整数x 和y(x和y均在int范围),代表这n个点的位置(点的编号从1到n);
  再接着是m个要求,每个要求2行,首先一行是一个k,表示这个要求和k个点有关,然后是顺序给出的k个点编号,代表小明不能走k1 -> k2 -> k3 ……-> ki这个顺序的路径;
  n 和 m等于0的时候输入结束。

  [Technical Specification]
  2 <= n <= 50
  1 <= m <= 100
  2 <= k <= 5

Output
  对于每个样例,如果存在满足要求的最短路径,请输出这个最短路径,结果保留两位小数;否则,请输出”Can not be reached!” (引号不用输出)。

Sample Input

3 1
1 1
2 1
3 1
2
1 2

2 1
0 0
1 1
2
1 2

5 3
0 0
5 3
1 2
1 22
5 21
3
1 2 3
2
4 5
2
1 5

0 0

Sample Output

2.00
Can not be reached!
21.65

Source
2013腾讯编程马拉松初赛第二场(3月22日)

Recommend
liuyiding | We have carefully selected several similar problems for you: 5209 5208 5207 5206 5205

告诉你某些路径不能存在,这是个很明显的提示,就是个自动机dp,dp[i][j]表示在自动机节点i,并且在点j时的最短路径,一开始我的循环方式可能不对,后来换了下就可以了,注意坐标还是直接用double 读入吧

/*************************************************************************
    > File Name: hdu4511.cpp
    > Author: ALex
    > Mail: zchao1995@gmail.com 
    > Created Time: 2015年04月16日 星期四 18时24分11秒
 ************************************************************************/

#include <functional>
#include <algorithm>
#include <iostream>
#include <fstream>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <stack>
#include <map>
#include <bitset>
#include <set>
#include <vector>

using namespace std;

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

const int MAX_NODE = 510;
int n;
double dp[MAX_NODE][55];

struct node
{
    double x, y;
}point[55];

double dist(node a, node b)
{
    double x = (a.x - b.x);
    double y = (a.y - b.y);
    return sqrt(x * x + y * y);
}

struct AC_Automation
{
    int next[MAX_NODE][55];
    int fail[MAX_NODE];
    int end[MAX_NODE];
    int root, L;

    int newnode()
    {
        for (int i = 0; i < n; ++i)
        {
            next[L][i] = -1;
        }
        end[L++] = 0;
        return L - 1;
    }

    void init ()
    {
        L = 0;
        root = newnode();
    }

    void Build_Trie (vector <int> buf)
    {
        int now = root;
        int len = buf.size();
        for (int i = 0; i < len; ++i)
        {
            if (next[now][buf[i]] == -1)
            {
                next[now][buf[i]] = newnode();
            }
            now = next[now][buf[i]];
        }
        end[now] = 1;
    }

    void Build_AC ()
    {
        queue <int> qu;
        fail[root] = root;
        for (int i = 0; i < n; ++i)
        {
            if (next[root][i] == -1)
            {
                next[root][i] = root;
            }
            else
            {
                fail[next[root][i]] = root;
                qu.push (next[root][i]);
            }
        }
        while (!qu.empty())
        {
            int now = qu.front();
            qu.pop();
            if (end[fail[now]])
            {
                end[now] = 1;
            }
            for (int i = 0; i < n; ++i)
            {
                if (next[now][i] == -1)
                {
                    next[now][i] = next[fail[now]][i];
                }
                else
                {
                    fail[next[now][i]] = next[fail[now]][i];
                    qu.push(next[now][i]);
                }
            }
        }
    }

    void solve()
    {
        for (int i = 0; i < L; ++i)
        {
            for (int j = 0; j < n; ++j)
            {
                dp[i][j] = (1LL << 60);
            }
        }
        dp[next[root][0]][0] = 0.0;
        for (int i = 0; i < n; ++i)
        {
            for (int j = 0; j < L; ++j)
            {
                if (dp[j][i] < (1LL << 60))
                {
                    for (int k = i + 1; k < n; ++k)
                    {
                        int nxt = next[j][k];
                        if (end[nxt])
                        {
                            continue;
                        }
                        dp[nxt][k] = min(dp[nxt][k], dp[j][i] + dist(point[k], point[i]));
                    }
                }
            }
        }
        double ans = (1LL << 60);
        for (int i = 0; i < L; ++i)
        {
            if (end[i] == 0)
            {
                ans = min(ans, dp[i][n - 1]);
            }
        }
        if (ans == (1LL << 60))
        {
            printf("Can not be reached!\n");
        }
        else
        {
            printf("%.2f\n", ans);
        }
    }

}AC;

vector <int> buf;

int main()
{
    int m;
    while (~scanf("%d%d", &n, &m))
    {
        if (!n && !m)
        {
            break;
        }
        AC.init();
        int tmp;
        for (int i = 0; i < n; ++i)
        {
            scanf("%lf%lf", &point[i].x, &point[i].y);
        }
        for (int i = 1; i <= m; ++i)
        {
            int k;
            buf.clear();
            scanf("%d", &k);
            while (k--)
            {
                scanf("%d", &tmp);
                --tmp;
                buf.push_back(tmp);
            }
            AC.Build_Trie(buf);
        }
        AC.Build_AC();
        AC.solve();
    }
    return 0;
}

hdu4511---小明系列故事——女友的考验(AC自动机+dp)

标签:ac自动机   dp   

原文地址:http://blog.csdn.net/guard_mine/article/details/45150521

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!