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. css hack 大全

    各个浏览器的css hack区别属性: IE6: _zoom:1; IE6/7: *zoom:1; IE6/7/8/9 :\9 各个浏览器的css hack区别规则 IE6: *html{} IE7: ...

  2. php 的一个pg_fetch_assoc的怪问题

    遇到过一种问题 . if($row=pg_fetch_assoc($result)){ while($row=pg_fetch_assoc($result)){ echo '3333'; $koCd ...

  3. chrome偶尔弹出新窗口的解决方案

    最近使用谷歌浏览器,在搜索页点击搜索结果时,偶尔会弹出新窗口,而不是新标签,试验发现,只要将chrome里面安装的google drive app卸载就行了. 当然了,如果此方法不适合你的情况,还可以 ...

  4. Win7使用IIS通过域名访问本地程序(网页、css、js等)

    一.目的:在本地浏览器里面,输入www.abc.com 可以访问我们本地搭建的网页程序 二.好处:在本地模拟,真实的访问,另外可以设置一些二级域名,例如static.abc.com域名用来存储像图片, ...

  5. 初学时的shell

    学习期间写过一些shell脚本, 测试过程:vi test.sh 后把程序写入其中,保存退出.然后改变文件属性:chmod +x test.sh 最后执行:./test.shfor语句测试:1)#!/ ...

  6. hibernate 对象状态异常:object references an unsaved transient instance - save the transient instance before flushing

    我的问题出在,删除的对象对应的表中有一个外键,关联着另外一个表,可是另外一个表中没有数据,所以报了这个错误. 参考http://www.cnblogs.com/onlywujun/archive/20 ...

  7. java开发规范总结_代码编码规范

    规范需要平时编码过程中注意,是一个慢慢养成的好习惯 1.基本原则 强制性原则:     1.字符串的拼加操作,必须使用StringBuilder:     2.try…catch的用法 try{ }c ...

  8. oraclesql日志

    select * from v$logfile;  select * from v$sql select sql_text,module,action,parsing_schema_name,firs ...

  9. Graphics类绘制图形

    1. 画直线 void drawLine(int startX,int startY,int endX,int endY); 四个参数分别为:起始点的x坐标和y坐标以及终点的x坐标和y坐标,该方法用于 ...

  10. UVA 11825 Hackers’ Crackdown(集合动态规划 子集枚举)

    Hackers’ Crackdown Miracle Corporations has a number of system services running in a distributed com ...