zoukankan      html  css  js  c++  java
  • HDU 4511 小明系列故事——女友的考验 (AC自动机+DP)

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

    Time Limit: 500/200 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
    Total Submission(s): 654    Accepted Submission(s): 136


    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

    一些路径不能走,通过AC自动机得到状态转移。

    之后dp[i][j] 表示在i点,状态在j的距离。

    网上看到一个floyed的做法,最后发现可以 cha掉,路径不能只看两个点的。如1->3  1->2->3  这样1是不能到3的。

    坐标int相减会溢出int,最好直接用double.

      1 /* ***********************************************
      2 Author        :kuangbin
      3 Created Time  :2014/3/2 22:07:17
      4 File Name     :E:2014ACM专题学习AC自动机HDU4511.cpp
      5 ************************************************ */
      6 
      7 #include <stdio.h>
      8 #include <string.h>
      9 #include <iostream>
     10 #include <algorithm>
     11 #include <vector>
     12 #include <queue>
     13 #include <set>
     14 #include <map>
     15 #include <string>
     16 #include <math.h>
     17 #include <stdlib.h>
     18 #include <time.h>
     19 using namespace std;
     20 const double INF = 1e20;
     21 int n;
     22 
     23 pair<int,int> p[100];
     24 double dis(pair<int,int>a,pair<int,int>b)//坐标相减爆int
     25 {
     26     return sqrt((double)(1.0 * a.first - b.first) * (1.0 * a.first - b.first) + (double)(1.0 * a.second - b.second)*(1.0 * a.second - b.second));
     27 }
     28 double dp[55][1000];
     29 void CheckMin(double &a,double b)
     30 {
     31     a = min(a,b);
     32 }
     33 struct Trie
     34 {
     35     int next[1000][55],fail[1000],end[1000];
     36     int root,L;
     37     int newnode()
     38     {
     39         for(int i = 1; i <= n;i++)
     40             next[L][i] = -1;
     41         end[L++] = 0;
     42         return L-1;
     43     }
     44     void init()
     45     {
     46         L = 0;
     47         root = newnode();
     48     }
     49     void insert(int a[],int cnt)
     50     {
     51         int now = root;
     52         for(int i  = 0;i < cnt;i++)
     53         {
     54             if(next[now][a[i]] == -1)
     55                 next[now][a[i]] = newnode();
     56             now = next[now][a[i]];
     57         }
     58         end[now] = 1;
     59     }
     60     void build()
     61     {
     62         queue<int>Q;
     63         fail[root] = root;
     64         for(int i = 1;i <= n;i++)
     65             if(next[root][i] == -1)
     66                 next[root][i] = root;
     67             else 
     68             {
     69                 fail[next[root][i]] = root;
     70                 Q.push(next[root][i]);
     71             }
     72         while(!Q.empty())
     73         {
     74             int now = Q.front();
     75             Q.pop();
     76             end[now] |= end[fail[now]];
     77             for(int i = 1;i <= n;i++)
     78                 if(next[now][i] == -1)
     79                     next[now][i] = next[fail[now]][i];
     80                 else
     81                 {
     82                     fail[next[now][i]] = next[fail[now]][i];
     83                     Q.push(next[now][i]);
     84                 }
     85         }
     86 
     87     }
     88     void solve()
     89     {
     90         for(int i = 1;i <= n;i++)
     91             for(int j = 0;j < L;j++)
     92                 dp[i][j] = INF;
     93         dp[1][next[root][1]] = 0;
     94         for(int i = 1;i < n;i++)
     95             for(int j = 0;j < L;j++)
     96                 if(dp[i][j] < INF)
     97                 {
     98                     for(int k = i+1;k <= n;k++)
     99                     {
    100                         int ss = next[j][k];
    101                         if(end[ss])continue;
    102                         CheckMin(dp[k][ss],dp[i][j] + dis(p[i],p[k]));
    103                     }
    104                 }
    105         double ans = INF;
    106         for(int i = 0;i < L;i++)
    107             if(dp[n][i] < INF)
    108                 CheckMin(ans,dp[n][i]);
    109         if(ans == INF)printf("Can not be reached!
    ");
    110         else printf("%.2f
    ",ans);
    111     }
    112 }ac;
    113 
    114 int a[10];
    115 int main()
    116 {
    117     //freopen("in.txt","r",stdin);
    118     //freopen("out.txt","w",stdout);
    119     int m;
    120     while(scanf("%d%d",&n,&m) == 2)
    121     {
    122         if(n == 0 && m == 0)break;
    123         for(int i = 1;i <= n;i++)
    124             scanf("%d%d",&p[i].first,&p[i].second);
    125         ac.init();
    126         int k;
    127         while(m--)
    128         {
    129             scanf("%d",&k);
    130             for(int i = 0;i < k;i++)
    131                 scanf("%d",&a[i]);
    132             ac.insert(a,k);
    133         }
    134         ac.build();
    135         ac.solve();
    136     }
    137     return 0;
    138 }
  • 相关阅读:
    .NET Core技术研究-通过Roslyn代码分析技术规范提升代码质量
    ASP.NET Core技术研究-全面认识Web服务器Kestrel
    .NET Core技术研究-主机Host
    ASP.NET Core技术研究-探秘依赖注入框架
    ASP.NET Core技术研究-探秘Host主机启动过程
    .NET Core技术研究-中间件的由来和使用
    深入浅出腾讯BERT推理模型--TurboTransformers
    深入浅出PyTorch(算子篇)
    深入浅出Transformer
    生产者消费者问题总结
  • 原文地址:https://www.cnblogs.com/kuangbin/p/3577678.html
Copyright © 2011-2022 走看看