您的位置:首页 > 其它

HDU 4511 小明系列故事——女友的考验 (AC自动机 + DP)

2017-10-06 21:22 344 查看
题意:略。

析:先把不能走的路径建立在AC自动机上,然后DP,dp[i][j] 表示当前在 i 结点,并且在 j 状态,然后再枚举就好,在建立AC自动机时,把违法状态都标记,在转移的时候路过这些状态。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#define debug() puts("++++");
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
//#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define all 1,n,1
#define FOR(i,x,n)  for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 50 + 10;
const int maxm = 1e5 + 10;
const int mod = 50007;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, -1, 0, 1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
}
const int maxnode = 100 * 5 + 100;
const int sigma = 55;
P a[maxn];
inline double dist(const P &lhs, const P &rhs){
return sqrt(sqr(lhs.fi*1.0-rhs.fi*1.0) + sqr(lhs.se*1.0-rhs.se*1.0));
}

double dp[maxn][maxnode];

struct Aho{
int ch[maxnode][sigma], f[maxnode];
int val[maxnode];
int sz;

void init(){ sz = 1;  ms(ch[0], 0); }

void insert(int *a, int len){
int u = 0;
for(int i = 0; i < len; ++i){
int c = a[i];
if(!ch[u][c]){
ms(ch[sz], 0);
val[sz] = 0;
ch[u][c] = sz++;
}
u = ch[u][c];
}
val[u] = 1;
}

void getFail(){
queue<int> q;
f[0] = 0;
for(int c = 0; c < sigma; ++c){
int u = ch[0][c];
if(u){ f[u] = 0; q.push(u);  }
}
while(!q.empty()){
int r = q.front();  q.pop();
for(int c = 0; c < sigma; ++c){
int u = ch[r][c];
if(!u){ ch[r][c] = ch[f[r]][c];  continue; }
q.push(u);
int v = f[r];
while(v && !ch[v][c])  v = f[v];
f[u] = ch[v][c];
val[u] |= val[f[u]];
}
}
}

double solve(){
for(int i = 0; i <= n; ++i)
for(int j = 0; j <= sz; ++j)
dp[i][j] = inf + 10;
dp[1][ch[0][1]] = 0;
for(int i = 1; i < n; ++i)
for(int j = 0; j < sz; ++j){
if(dp[i][j] >= inf)  continue;
for(int k = i+1; k <= n; ++k){
int nxt = ch[j][k];
if(val[nxt])  continue;
dp[k][nxt] = min(dp[k][nxt], dp[i][j] + dist(a[i], a[k]));
}
}
double ans = inf + 10;
for(int i = 0; i < sz; ++i)
ans = min(ans, dp
[i]);
return ans;
}
};
Aho aho;

int b[10];

int main(){
while(scanf("%d %d", &n, &m) == 2 && n+m){
for(int i = 1; i <= n; ++i)  scanf("%d %d", &a[i].fi, &a[i].se);
aho.init();
for(int i = 0; i < m; ++i){
int len;
scanf("%d", &len);
for(int j = 0; j < len; ++j)  scanf("%d", b+j);
aho.insert(b, len);
}
aho.getFail();
double ans = aho.solve();
if(ans >= inf)  puts("Can not be reached!");
else  printf("%.2f\n", ans);
}
return 0;
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: