综合(奇技淫巧):HDU 5118 GRE Words Once More!

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时用手写栈防爆栈,有个必要的优化,就是扫过后答案是可以重复利用的。

 1 #include <algorithm>
 2 #include <iostream>
 3 #include <cstring>
 4 #include <cstdio>
 5 #include <vector>
 6 using namespace std;
 7 const int N=200010,M=100000000;
 8 vector<pair<int,int> >g[N];
 9 int ans[M+10],f[N],be[N],ed[N],tot;
10 int st[N],dep[N],vis[N],mem[N],top;
11 int T,cas=0,q,n,m,Q;
12 int main(){
13     scanf("%d",&T);
14     while(T--){
15         scanf("%d%d%d",&n,&m,&Q);tot=0;
16         for(int i=2;i<=n;i++)scanf("%d",&f[i]);
17         for(int i=1,a,b,v;i<=m;i++){
18             scanf("%d%d%d",&a,&b,&v);
19             g[a].push_back(make_pair(v,b));
20         }
21         for(int i=1;i<=n;i++)
22             sort(g[i].begin(),g[i].end());
23         st[top=1]=1;dep[top]=0;
24         memset(vis,0,sizeof(vis));
25         memset(be,0,sizeof(be));
26         memset(ed,0,sizeof(ed));
27         while(top){
28             int x=st[top],d=dep[top];
29             if(vis[top]){
30                 if(!ed[x])ed[x]=tot;
31                 vis[top]=0;top-=1;
32                 continue;
33             }
34             vis[top]=1;
35             if(be[x]){
36                 int depth=-mem[x]+d;
37                 for(int i=be[x];i<=ed[x];i++){
38                     ans[++tot]=ans[i]+depth;
39                     if(tot>=M)break;
40                 }if(tot>=M)break;
41                 continue;
42             }
43             be[x]=tot+1;mem[x]=d;
44             if(f[x])ans[++tot]=d;
45             if(tot>=M)break;
46             for(int i=g[x].size()-1;~i;i--){
47                 st[++top]=g[x][i].second;
48                 dep[top]=d+1;
49             }
50         }
51         printf("Case #%d:\n",++cas);
52         while(Q--){
53             scanf("%d",&q);
54             if(q>tot)printf("-1\n");
55             else printf("%d\n",ans[q]);
56         }
57         for(int i=1;i<=n;i++)g[i].clear();
58     }
59     return 0;
60 }
时间: 2024-10-24 17:43:01

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

hdu 4787 GRE Words Revenge 在线AC自动机

hdu 4787 GRE Words Revenge Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 327680/327680 K (Java/Others)Total Submission(s): 2505    Accepted Submission(s): 614 Problem Description Now Coach Pang is preparing for the Graduate Record Examina

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

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4787 题目大意: 给你若干个单词,查询一篇文章里出现的单词数.. 就是被我水过去的...暴力重建AC自动机- -然后暴力查找.. 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <queue> 5 #include <map> 6 #include

●HDU 4787 GRE Words Revenge

题链: http://acm.hdu.edu.cn/showproblem.php?pid=4787 题解: AC自动机(强制在线构造) 题目大意: 有两种操作, 一种为:+S,表示增加模式串S, 另一种为:?S,表示查询S中有多少子串为已经给出的模式串. (同时由于输入根据上一次的答案加密 ,所以强制在线) (事先提一下,对于多次给出的相同模式串,是要去重的,至于怎么去重,就随便用trie树或者map+string就好了.) 进入正题: 难道真的要让AC自动机变得在线起来么? 其实还是用普通A

Hdu 4117 GRE Words (后缀数组+dp)

题目大意: 求出最多能记住的单词的权值和,要求最大. 记住的规则就是上一个单词是这个单词的子串. 思路分析: 首先得声明这题是数据水了才能用sa做的. sa的复杂度最多可以达到 Orz(sumlen * sumlen) ... 所以我们sa处理的就是这个串是否是下一个串的子串,如果是就转移方程. dp[i] = max (dp[i] , dp[j] + val[i])... #include <cstdio> #include <iostream> #include <cst

[后缀数组+dp/AC自动机+dp+线段树] hdu 4117 GRE Words

题意: 给你N个字符串, N(1 <= N <= 2w), 所有串的长度加一起不超过30w.每个串有个值.这个值[-1000, 1000]. 问不打乱字符串顺序,从中取若干个字符串,使得前一个串是后一个串的子串,求满足前面调条件的字符串值得和最大,求这个值. 思路: 其实就是一个很明显的dp. dp[i]代表以第i个字符串结尾的最大权值. 但是就是子串这个问题怎么处理. 由于这题数据比较水可以用后缀数组处理这个问题. 将所有字符串拼接,做sa. 每次在height数组里往上和往下寻找公共前缀等

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

本题解不包括个人觉得太水的题(J题本人偷懒没做). 个人觉得这场其实HDU-5116要比HDU-5118难,不过赛场情况似乎不是这样.怀疑是因为老司机带错了路. 这套题,个人感觉动态规划和数论是两个主要的考点. HDU 5113 Black And White HDU 5114 Collision HDU 5116 Everlasting L HDU 5117 Fluorescent HDU 5118 GRE Words Once More!

AC自动机- 自我总结

AC自动机算法总结  No.1 What's Aho-Corasick automaton? 一种多模式串匹配算法,该算法在1975年产生于贝尔实验室,是著名的多模匹配算法之一. 简单的说,KMP用来匹配一个模式串:但如果现在有多个模式串需要在同一篇文章中出现,现在就需要Aho-Corasick automaton算法了. 不要天真的以为AC自动机为auto-Accept,虽然他能让你AC一些题. No.2 My Understanding About Aho-Corasick automato

第九周 10.25-10.31

10.25 HDU 4117 GRE Words 卡了很久的一个题目.比较综合. 看了很久题解还是各种写挫. 1 #pragma comment(linker, "/STACK:102400000,102400000") 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <algorithm> 6 #include <queue>

[GodLove]Wine93 Tarining Round #4

比赛链接: http://acm.hust.edu.cn/vjudge/contest/view.action?cid=44903#overview 题目来源: 2011 Asia ChengDu Regional Contest Wine93有话说: 爆0, oh-year!   ID Origin Title   13 / 118 Problem A HDU 4111 Alice and Bob 137 / 358 Problem B HDU 4112 Break the Chocolate