GRE Words Once More!

Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others)
Total Submission(s): 205    Accepted Submission(s): 32

Problem Description
Now Matt is preparing for the Graduate Record Examinations as Coach Pang did in 2013 and George did in 2011.

Thanks to modern techniques, Matt uses automata instead of old-fasioned vocabulary books.

The automata used by Matt is a directed acyclic graph (DAG) with N vertices and M edges. The vertices are conveniently numbered by 1, 2, . . . , N . Each edge is labeled with an integer. Additionally, some vertices are marked as special.

A GRE word is obtained by concatenating the labels on the path from vertex 1 to a special vertex.

Now, Matt has Q questions. The i-th question is asking for the length of ki-th smallest words among all the GRE words he can obtain in lexicographical order.

 
Input
The first line contains only one integer T , which indicates the number of test cases.

For each test case, the first line contains three integers N, M, Q (2 ≤ N ≤ 105, 0 ≤ M ≤ 105, 1 ≤ Q ≤ 105).

The second line contains N - 1 integers s2, . . . , sn. If the i-th vertex is special, then si = 1. Otherwise, si = 0. Vertex 1 is never special.

Each of the following M lines contains three integers ai, bi, ci denoting an edge from vertex ai to vertex bi labeled with ci (1 ≤ ai, bi ≤ N, 1 ≤ ci ≤ 109). For each vertex v, all outgoing edges are labeled with distinct integers.

Each of the following Q lines contains the integer ki (1 ≤ ki ≤ 108) of the i-th question.

 
Output
For each test case, output “Case #x:” in the frirst line, where x is the case number (starting from 1).

Then, for each question, output the length of the word in one line. If the word does not exist, output “-1” (without quotes) instead.

 
Sample Input
1
3 3 4
1 1
1 2 1
1 3 12
2 3 3
1
2
3
4
 
Sample Output
Case #1:
1
2
1
-1

Hint

There are 3 GRE words in total (sorted in lexicographical order):
1. (1)
2. (1, 3)
3. (12)

  这道题不是很难,需要注意清空数组。
  思路是预处理答案,DFS时用手写栈防爆栈,有个必要的优化,就是扫过后答案是可以重复利用的。
 #include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
using namespace std;
const int N=,M=;
vector<pair<int,int> >g[N];
int ans[M+],f[N],be[N],ed[N],tot;
int st[N],dep[N],vis[N],mem[N],top;
int T,cas=,q,n,m,Q;
int main(){
scanf("%d",&T);
while(T--){
scanf("%d%d%d",&n,&m,&Q);tot=;
for(int i=;i<=n;i++)scanf("%d",&f[i]);
for(int i=,a,b,v;i<=m;i++){
scanf("%d%d%d",&a,&b,&v);
g[a].push_back(make_pair(v,b));
}
for(int i=;i<=n;i++)
sort(g[i].begin(),g[i].end());
st[top=]=;dep[top]=;
memset(vis,,sizeof(vis));
memset(be,,sizeof(be));
memset(ed,,sizeof(ed));
while(top){
int x=st[top],d=dep[top];
if(vis[top]){
if(!ed[x])ed[x]=tot;
vis[top]=;top-=;
continue;
}
vis[top]=;
if(be[x]){
int depth=-mem[x]+d;
for(int i=be[x];i<=ed[x];i++){
ans[++tot]=ans[i]+depth;
if(tot>=M)break;
}if(tot>=M)break;
continue;
}
be[x]=tot+;mem[x]=d;
if(f[x])ans[++tot]=d;
if(tot>=M)break;
for(int i=g[x].size()-;~i;i--){
st[++top]=g[x][i].second;
dep[top]=d+;
}
}
printf("Case #%d:\n",++cas);
while(Q--){
scanf("%d",&q);
if(q>tot)printf("-1\n");
else printf("%d\n",ans[q]);
}
for(int i=;i<=n;i++)g[i].clear();
}
return ;
}

综合(奇技淫巧):HDU 5118 GRE Words Once More!的更多相关文章

  1. HDU 5118 GRE Words Once More!

    题目链接:HDU-5118 题意:给定一个有向无环图,每条边有一个权值.标定一些特定节点为“特殊节点”.从节点1出发到某“特殊节点”结束的路径,称为一个“GRE单词”.单词由路径上的权值组成.给定一组 ...

  2. [HDU 4787] GRE Words Revenge (AC自动机)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4787 题目大意: 给你若干个单词,查询一篇文章里出现的单词数.. 就是被我水过去的...暴力重建AC自 ...

  3. hdu 4117 -- GRE Words (AC自动机+线段树)

    题目链接 problem Recently George is preparing for the Graduate Record Examinations (GRE for short). Obvi ...

  4. ●HDU 4787 GRE Words Revenge

    题链: http://acm.hdu.edu.cn/showproblem.php?pid=4787 题解: AC自动机(强制在线构造) 题目大意: 有两种操作, 一种为:+S,表示增加模式串S, 另 ...

  5. hdu 4117 GRE Words AC自动机DP

    题目:给出n个串,问最多能够选出多少个串,使得前面串是后面串的子串(按照输入顺序) 分析: 其实这题是这题SPOJ 7758. Growing Strings AC自动机DP的进阶版本,主题思想差不多 ...

  6. hdu 4117 GRE Words (ac自动机 线段树 dp)

    参考:http://blog.csdn.net/no__stop/article/details/12287843 此题利用了ac自动机fail树的性质,fail指针建立为树,表示父节点是孩子节点的后 ...

  7. HDU 4787 GRE Words Revenge

    Description Now Coach Pang is preparing for the Graduate Record Examinations as George did in 2011. ...

  8. HDU 4117 GRE Words

    这道题不难想到这样的dp. dp[字符串si] = 以si为结尾的最大总权值. dp[si] = max(dp[sj]) ,1.j < i,2.sj是si的子串. 对于第二个条件,是一个多模版串 ...

  9. 2014ACM/ICPC亚洲区北京站题解

    本题解不包括个人觉得太水的题(J题本人偷懒没做). 个人觉得这场其实HDU-5116要比HDU-5118难,不过赛场情况似乎不是这样.怀疑是因为老司机带错了路. 这套题,个人感觉动态规划和数论是两个主 ...

随机推荐

  1. 关于 ORA - 01861 文字与格式字符串不匹配问题(oracle存储过程)

    一般问题(TO_DATE 和 TO_CHAR 两种格式互换)比如: 只要转化下格式就OK ,这里就不详细解释这两种格式的用法了! 今天把之前做好的模块拿到当地实习,不管怎么测 ,连续测试了好几个存储过 ...

  2. struts通过Ajax返回数据时,例如对象类型,没有执行Ajax的回调函数

    <result type="json"  name="success">                 <param name=" ...

  3. strace跟踪操作的详细内容

  4. CorAnimation7-高效绘图、图像IO以及图层性能

    高效绘图 软件绘图 术语绘图通常在Core Animation的上下文中指代软件绘图(意即:不由GPU协助的绘图).在iOS中,软件绘图通常是由Core Graphics框架完成来完成.但是,在一些必 ...

  5. java封装和多态

    封装.集成.多态和抽象是java的基本特征. 封装的第一步就是对类进行组装,即定义一个类,这时候要考虑这个类要有哪些属性.方法等.第二步就是信息的隐藏,这包括访问修饰符.get/set方法和某些特定方 ...

  6. boost 1.56.0 编译

    编译步骤及参数说明: http://www.cnblogs.com/zhcncn/p/3950477.html 编译64位版本: http://www.cnblogs.com/codingmylife ...

  7. django基本命令备忘录

    1. 新建一个 django project django-admin.py startproject project-name 新建 app python manage.py startapp ap ...

  8. C#让程序自动在管理员权限下运行

    windows 7和vista提高的系统的安全性,同时需要明确指定“以管理员身份运行”才可赋予被运行软件比较高级的权限,比如访问注册表等.否则,当以普通身份运行的程序需要访问较高级的系统资源时,将会抛 ...

  9. 用VS2010编写的C++程序,在其他电脑上无法运行,提示缺少mfc100.dll的解决办法

    问题: 在自己电脑上用VS2010编写的VC++程序(使用MFC库),不能在其他电脑上运行.双击提示: "无法启动此程序,因为计算机中丢失mfc100.dll 尝试重新安装该程序以解决此问题 ...

  10. Zoj 3868 GCD Expectation

    给一个集合,大小为n , 求所有子集的gcd 的期望和 . 期望的定义为 这个子集的最大公约数的K次方 : 每个元素被选中的概率是等可能的 即概率 p = (发生的事件数)/(总的事件数); 总的事件 ...