链接

这题没想到怎么做,问了下p队长,大悟。。

先求出任意两串的在trie树上的最短距离,期间是不能走到不合法的地方,我是用spfa求得,在更新和加入节点时判断一下是不是合法位置。

求出最短距离之后,找出一条从0出发遍历所有串的最短距离,可以dp出,dp[i][j]表示当前状态以节点j串结尾的最短距离。

枚举一下最后结尾的为哪一个串时距离最短。

 #include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
#include<vector>
#include<cmath>
#include<queue>
#include<set>
using namespace std;
#define N 60010
#define M 10010
#define LL long long
#define INF 0xfffffff
const double eps = 1e-;
const double pi = acos(-1.0);
const double inf = ~0u>>;
const int child_num = ;
int w[][];
bool vis[N];
int dis[N];
class AC
{
private:
int ch[N][child_num];
int fail[N];
int Q[N];
int val[N],vv[N];
int id[];
int sz ;
int po[];
int dp[][];
public:
void init()
{
fail[] = ;
id[''] = ;
id[''] = ;
}
void reset()
{
memset(ch[],,sizeof(ch[]));
memset(val,,sizeof(val));
memset(vv,,sizeof(vv));
sz=;
}
void insert(char *a ,int key)
{
int p = ;
for( ; *a ; a++)
{
int d = id[*a];
if(ch[p][d]==)
{
memset(ch[sz],,sizeof(ch[sz]));
ch[p][d] = sz++;
}
p = ch[p][d];
}
if(key!=-)
{
val[p] = <<(key-);
po[key] = p;
}
else
{
vv[p] = ;
}
}
void construct()
{
int i,head=,tail = ;
for(i = ; i < child_num ; i++)
if(ch[][i])
{
fail[ch[][i]] = ;
Q[tail++] = ch[][i];
}
while(head!=tail)
{
int u = Q[head++];
vv[u]|=vv[fail[u]];
val[u]|=val[fail[u]];
for(i = ;i < child_num ; i++)
{
if(ch[u][i])
{
fail[ch[u][i]] = ch[fail[u]][i];
Q[tail++] = ch[u][i];
}
else ch[u][i] = ch[fail[u]][i];
}
}
}
int spfa(int st,int ed)
{
int i;
memset(vis,,sizeof(vis));
for(i = ; i <= sz ;i++)
dis[i] = INF;
dis[st] = ;
vis[st] = ;
queue<int>q;
q.push(st);
while(!q.empty())
{
int u = q.front();
q.pop();
for(i = ; i < child_num; i++)
{
int v = ch[u][i];
if(vv[v]) continue;
dis[v] = min(dis[v],dis[u]+);
//if(st == 4&&ed==8)
//cout<<v<<" "<<dis[v]<<" "<<u<<endl;
if(!vis[v])
{
vis[v] = ;
q.push(v);
}
}
}
return dis[ed];
}
void find_dis(int n)
{
int i,j;
po[] = ;
for(i = ; i <= n ;i++)
{
w[][i] = w[i][] = spfa(,po[i]);
//cout<<w[0][i]<<endl;
}
for(i = ; i <= n; i++)
for(j = ; j <= n ;j++)
{
w[i][j] = spfa(po[i],po[j]);
// cout<<w[i][j]<<" "<<i<<" "<<j<<endl;
}
}
void work(int n)
{
int i,j,g;
for(i = ; i < (<<n) ; i++)
for(j = ;j <= n; j++)
dp[i][j] = INF;
dp[][] = ;
for(i = ;i < (<<n) ; i++)
{
for(j = ;j <= n ;j++)
{
for(g = ; g <= n ;g++)
{
if((<<(g-))&i) continue;
dp[i+(<<(g-))][g] = min(dp[i+(<<(g-))][g],dp[i][j]+w[j][g]);
}
}
}
int ans = INF;
for(i = ; i <= n ; i++)
ans = min(ans,dp[(<<n)-][i]);
cout<<ans<<endl;
}
}ac;
char vir[],re[];
int main()
{
int n,m,i;
ac.init();
while(scanf("%d%d",&n,&m)&&n&&m)
{
ac.reset();
for(i = ; i <= n ;i++)
{
scanf("%s",re);
ac.insert(re,i);
}
while(m--)
{
scanf("%s",vir);
ac.insert(vir,-);
}
ac.construct();
ac.find_dis(n);
ac.work(n);
}
return ;
}

hdu3247Resource Archiver(ac自动机+spfa)的更多相关文章

  1. HDU3247 Resource Archiver (AC自动机+spfa+状压DP)

    Great! Your new software is almost finished! The only thing left to do is archiving all your n resou ...

  2. hdu3247Resource Archiver (AC自动机+最短路+状压dp)

    Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Others) Total Submis ...

  3. 【hdu3247-Resource Archiver】位压DP+AC自动机+SPFA

    题意:给定n个文本串,m个病毒串,文本串重叠部分可以合并,但合并后不能含有病毒串,问所有文本串合并后最短多长. (2 <= n <= 10, 1 <= m <= 1000) 题 ...

  4. hdu_3247_Resource Archiver(AC自动机+bfs+TSP)

    题目链接:hdu_3247_Resource Archiver 题意: 有n个资源串,m个病毒串,现在要将所有的资源串整合到一个串内,并且这个串不能包括病毒串,问最短的串长为多少 题解: 将资源串和病 ...

  5. HDU3247 Resource Archiver —— AC自动机 + BFS最短路 + 状压DP

    题目链接:https://vjudge.net/problem/HDU-3247 Resource Archiver Time Limit: 20000/10000 MS (Java/Others)  ...

  6. HDU - 3247 Resource Archiver (AC自动机,状压dp)

    \(\quad\)Great! Your new software is almost finished! The only thing left to do is archiving all you ...

  7. HDU 3247 Resource Archiver (AC自动机+BFS+状压DP)

    题意:给定 n 个文本串,m个病毒串,文本串重叠部分可以合并,但合并后不能含有病毒串,问所有文本串合并后最短多长. 析:先把所有的文本串和病毒都插入到AC自动机上,不过标记不一样,可以给病毒标记-1, ...

  8. 【HDU3247】 Resource Archiver(DP+AC自动机+最短路)

    Resource Archiver Time Limit: 10000MS   Memory Limit: 100000KB   64bit IO Format: %I64d & %I64u ...

  9. HDU-3247 Resource Archiver(AC自动机+BFS)

    Description Great! Your new software is almost finished! The only thing left to do is archiving all ...

随机推荐

  1. Why MySQL could be slow with large tables ?

    https://www.percona.com/blog/2006/06/09/why-mysql-could-be-slow-with-large-tables/

  2. JDBC 基本操作

    1. 简介 JDBC(Java DataBase Connectivity) 是有一些接口和类构成的API JDBC是J2SE的一部分, 又java.sql: javax.sql包组成.   应用程序 ...

  3. JS之原型对象

    1.__proto__ 每个对象都有一个__proto__属性,指向该对象的原型对象 <script> var person = function(name,city){ this.nam ...

  4. wpa gui

    wpa gui是wpa_supplicant的ui工具. wpa_supplicant源码中包含了wpa_gui, 在目录wpa_gui-qt4中. 先运行wpa supplicant,再运行wpa ...

  5. Android EditText组件drawableLeft属性设置的图片和hint设置的文字之间的距离

    有的时候,我们需要在文本框里放置icon图片,并且设置默认提示文字的时候,需要设置两者之间的间距,如下图: 这里想设置的就是之前的手机icon和”请输入手机号“之间的距离,则可是使用以下的方式: &l ...

  6. DIA

    Dia Tutorial: http://www.seanet.com/~hgg9140/comp/diatut/all/all.html Dia Tutorial

  7. 解决 git 中文路径显示 unicode 代码的问题

    解决 git 中文路径显示 unicode 代码的问题 当被修改的文件中带有中文字符时,中文字符会被转换为 unicode 代码,看不出原来的文件名. 这时,只要配置 :: git config -- ...

  8. XML中文本节点存储任意字符的方法

    XML xml是一种可扩展标签语言, 为众多浏览器支持解析, ajax更是利用xml来完成服务器和客户端之前的通信. xml基本元素为 <label>xxx</label>, ...

  9. LUA 函数式编程demo

    什么是函数式编程 http://www.zhihu.com/topic/19585411/hot 函数式编程的本质函数式编程中的函数这个术语不是指计算机中的函数(实际上是Subroutine),而是指 ...

  10. C# 中using的几个用途

    参考文献 http://www.cnblogs.com/morningwang/archive/2008/03/12/1102952.html http://msdn.microsoft.com/zh ...