标签:12c spl min mem title 字符 电影院 alt 打破
#include<cstdio> #include<iostream> #include<algorithm> #include<cstring> #include<queue> #include<vector> #include<cmath> using namespace std; const int MAXN = 6000; const int MAXM = 600; const double INF = 1e18; int trie[600][60],cnt; int fail[600],val[600],n,m,k; double dp[60][600]; struct node { double x, y; }a[60]; void insert(double *v) { int now = 0; for (int i = 1; i <=k; i++) { int u = v[i]; if (!trie[now][u]) { trie[now][u] = ++cnt; } now = trie[now][u]; } val[now]=1; } void get_fail() { int now = 0; queue<int> q; for (int i = 1; i <=n; i++) if (trie[now][i]) { fail[trie[now][i]] = 0; q.push(trie[now][i]); } while (!q.empty()) { int u = q.front(); q.pop(); if (val[fail[u]]) val[u]=1; for (int i = 1; i <=n; i++) { int v = trie[u][i]; if (v) { fail[v] = trie[fail[u]][i]; q.push(v); } else { trie[u][i] = trie[fail[u]][i]; } } } return; } double dis(int i, int j) { double tmp = (a[i].x - a[j].x)*(a[i].x - a[j].x) + (a[i].y - a[j].y)*(a[i].y - a[j].y); return sqrt(tmp); } double v[600]; double solve() { double minn = INF; for (int i = 1; i <= n; i++) for (int j = 0; j <= cnt; j++) dp[i][j] = INF; dp[1][trie[0][1]] = 0; for (int i = 1; i <= n; i++) { for (int j = 0; j <= cnt; j++) { if (dp[i][j]<INF) { for (int k = i + 1; k <= n; k++) if (!val[trie[j][k]]) dp[k][trie[j][k]] = min(dp[k][trie[j][k]], dp[i][j] + dis(i, k)); } } } for(int i=0;i<=cnt;i++) minn = min(minn, dp[n][i]); return minn; } int main() { while (scanf("%d%d", &n, &m) && n && m) { cnt = 0; memset(trie, 0, sizeof(trie)); memset(val, 0, sizeof(val)); memset(fail, 0, sizeof(fail)); for (int i = 1; i <= n; i++) scanf("%lf%lf", &a[i].x, &a[i].y); while(m--) { scanf("%d", &k); for (int i = 1; i <= k; i++) scanf("%lf", &v[i]); insert(v); } get_fail(); double ans = solve(); if (ans >= INF) printf("Can not be reached!\n"); else printf("%.2f\n", ans); } return 0; }
【HDU 4511】小明系列故事——女友的考验(AC自动机+DP)
标签:12c spl min mem title 字符 电影院 alt 打破
原文地址:https://www.cnblogs.com/rentu/p/11336071.html