标签:sea pac ac自动机 bool trie ble http 必须 范围
Time Limit: 500/200 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 1734 Accepted Submission(s): 466
#include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #include <queue> #include <cmath> using namespace std; #define INF 0x3f3f3f3f double a[55][55]; double dp[55][5100]; int buf[55]; int n,m,len; struct Trie { int Next[5100][55];//26是这里讨论26个小写字母的情况,根据情况修改 int fail[5100]; bool end[5100];//end数组表示以该节点结尾的字符串的数量 int root,L;//L用来标记节点序号,以广度优先展开的字典树的序号 int newnode() //建立新节点 { for(int i = 1;i <=n;i++) Next[L][i] = -1; //将该节点的后继节点域初始化 end[L++] = 0; return L-1; //返回当前节点编号 } void init() //初始化操作 { L = 0; root = newnode(); } void insert(int buf[],int len) { int now = root; 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]=true;//以该节点结尾的字符串数量增加1 } void build() { queue<int>Q; //用广度优先的方式,将树层层展开 fail[root] = root; for(int i = 1;i <=n;i++) if(Next[root][i] == -1) Next[root][i] = root; else { fail[Next[root][i]] = root; Q.push(Next[root][i]); } while( !Q.empty() ) { int now = Q.front(); Q.pop(); if(end[fail[now]])end[now]=true; for(int i = 1;i <=n;i++) if(Next[now][i] == -1) Next[now][i] = Next[fail[now]][i];//该段的最后一个节点匹配后,跳到拥有最大公共后缀的fail节点继续匹配 else { fail[Next[now][i]]=Next[fail[now]][i];//当前节点的fail节点等于它前驱节点的fail节点的后继节点 Q.push(Next[now][i]); } } } void solve() { double minn=1e18; for(int i=0;i<=n;i++) for(int j=0;j<L;j++) dp[i][j]=1e18; dp[0][0]=0; for(int i=0;i<n;i++) for(int j=0;j<L;j++) { if(dp[i][j]<1e18) { for(int k=i+1;k<=n;k++) { if(i==0) k=1; int news=Next[j][k]; if(end[news])continue; if(dp[k][news]>dp[i][j]+a[i][k]) dp[k][news]=dp[i][j]+a[i][k]; if(k==n&&dp[k][news]<minn) minn=dp[k][news]; // cout<<lead[i][j]<<" "<<" "<<j<<" "<<k<<" "<<news<<" "<<dp[i+1][news]<<endl; if(k==1) break; } } } if(minn<1e18-5) printf("%.2f\n",minn); else puts("Can not be reached!"); } }; Trie ac; struct node { double x; double y; }; node poi[55]; int main() { while(scanf("%d%d",&n,&m)!=EOF) { ac.init(); if(n==0&&m==0)break; for(int i=1;i<=n;i++) scanf("%lf%lf",&poi[i].x,&poi[i].y); for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { a[i][j]=sqrt((poi[i].x-poi[j].x)*(poi[i].x-poi[j].x)+(poi[i].y-poi[j].y)*(poi[i].y-poi[j].y)); } } while(m--) { scanf("%d",&len); for(int i=0;i<len;i++) scanf("%d",&buf[i]); ac.insert(buf,len); } ac.build(); ac.solve(); } return 0; }
HDU 4511 小明系列故事——女友的考验 (AC自动机+DP)
标签:sea pac ac自动机 bool trie ble http 必须 范围
原文地址:http://www.cnblogs.com/a249189046/p/7593008.html