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. ASCII 对应表 CHR()

    chr(9) tab空格       chr(10) 换行      chr(13) 回车        chr(13)&chr(10) 回车换行       chr(32) 空格符      ...

  2. C# DateTime显示时间格式的使用

    代码DateTime.ToString() Patterns All the patterns: 0 MM/dd/yyyy 08/22/2006 1 dddd, dd MMMM yyyy Tuesda ...

  3. ==和equals

  4. 停止Java线程,小心interrupt()方法

    来源:http://blog.csdn.net/wxwzy738/article/details/8516253 程序是很简易的.然而,在编程人员面前,多线程呈现出了一组新的难题,如果没有被恰当的解决 ...

  5. 获取SqlServer当前链接数

    1.提供有关 Microsoft SQL Server 数据库引擎实例中的当前用户.会话和进程的信息,显示所有session sp_who 2.针对 SQL Server 上的每个经过身份验证的会话返 ...

  6. oracle查询最占用资源的查询

    从V$SQLAREA中查询最占用资源的查询 select b.username username,a.disk_reads reads,a.executions exec,a.disk_reads/d ...

  7. Oracle 的merge into 用法

    1.merge into的用途 Merge是一个非常有用的功能,与DB2中的merge into功能几乎一样,与Mysql里的insert into on duplicate key也很类似.MERG ...

  8. ios 添加通用断点定位到异常点

    今天下午项目突然异常崩溃,因为代码没有多少结构改动,恢复旧版本还是不行,判定为网络获取信息异常,无奈从网络获取的信息太多,搞了很久后发现有个通用异常断点很好用,新手就是新手,浪费时间了.

  9. 从1到n整数中1出现的次数

    题目如题 如 5 中1出现的次数 为1 12中1出现的次数为5 public class NumberOf1Between1AndN { /* *输入一个整数n,求从1到n这N个十进制表示中1出现的次 ...

  10. ACM hdu 1008 Elavator

    Elevator其实是一道水题,思路也很简单,但不知道怎么也不能AC,后来看了别人的再比较自己的以后找到错误. 在判断奇偶数之后的语句时,我用了if()  else if(),这是不能AC的原因,这种 ...