综合(奇技淫巧):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
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.
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.
Then, for each question, output the length of the word in one line. If the word does not exist, output “-1” (without quotes) instead.
3 3 4
1 1
1 2 1
1 3 12
2 3 3
1
2
3
4
1
2
1
-1
There are 3 GRE words in total (sorted in lexicographical order):
1. (1)
2. (1, 3)
3. (12)
#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!的更多相关文章
- HDU 5118 GRE Words Once More!
题目链接:HDU-5118 题意:给定一个有向无环图,每条边有一个权值.标定一些特定节点为“特殊节点”.从节点1出发到某“特殊节点”结束的路径,称为一个“GRE单词”.单词由路径上的权值组成.给定一组 ...
- [HDU 4787] GRE Words Revenge (AC自动机)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4787 题目大意: 给你若干个单词,查询一篇文章里出现的单词数.. 就是被我水过去的...暴力重建AC自 ...
- hdu 4117 -- GRE Words (AC自动机+线段树)
题目链接 problem Recently George is preparing for the Graduate Record Examinations (GRE for short). Obvi ...
- ●HDU 4787 GRE Words Revenge
题链: http://acm.hdu.edu.cn/showproblem.php?pid=4787 题解: AC自动机(强制在线构造) 题目大意: 有两种操作, 一种为:+S,表示增加模式串S, 另 ...
- hdu 4117 GRE Words AC自动机DP
题目:给出n个串,问最多能够选出多少个串,使得前面串是后面串的子串(按照输入顺序) 分析: 其实这题是这题SPOJ 7758. Growing Strings AC自动机DP的进阶版本,主题思想差不多 ...
- hdu 4117 GRE Words (ac自动机 线段树 dp)
参考:http://blog.csdn.net/no__stop/article/details/12287843 此题利用了ac自动机fail树的性质,fail指针建立为树,表示父节点是孩子节点的后 ...
- HDU 4787 GRE Words Revenge
Description Now Coach Pang is preparing for the Graduate Record Examinations as George did in 2011. ...
- HDU 4117 GRE Words
这道题不难想到这样的dp. dp[字符串si] = 以si为结尾的最大总权值. dp[si] = max(dp[sj]) ,1.j < i,2.sj是si的子串. 对于第二个条件,是一个多模版串 ...
- 2014ACM/ICPC亚洲区北京站题解
本题解不包括个人觉得太水的题(J题本人偷懒没做). 个人觉得这场其实HDU-5116要比HDU-5118难,不过赛场情况似乎不是这样.怀疑是因为老司机带错了路. 这套题,个人感觉动态规划和数论是两个主 ...
随机推荐
- grunt 构建工具(build tool)初体验
操作环境:win8 系统,建议使用 git bash (window下的命令行工具) 1,安装node.js 官网下载:https://nodejs.org/ 直接点击install ,会根据你的操 ...
- 继续(3n+1)猜想
卡拉兹(Callatz)猜想已经在1001中给出了描述.在这个题目里,情况稍微有些复杂. 当我们验证卡拉兹猜想的时候,为了避免重复计算,可以记录下递推过程中遇到的每一个数.例如对n=3进行验证的时候, ...
- 自定义Operation
1.要自定义一个Operation 首先要创建一个继承于NSOperation的类. 2.在创建好的类的.h文件声明自定义的方法:-(instancetype)initWithDownLoadMess ...
- Oracle 日期类型timestamp(时间戳)和date类型使用
body { font-family: "Microsoft YaHei UI","Microsoft YaHei",SimSun,"Segoe UI ...
- wamp使用方法【总】
Apache与php配置:我们把php-5.2.9-Win32.zip解压到C盘根目录下,把文件夹名字改成PHP,这样方便一下. 1. 找到PHP目录下的“php.ini-dist”或者“php.in ...
- jQuery 个人随笔
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- js获取天气
我们经常看到有的网站显示天气信息,它是怎么做出来的呢?今天就来分享一些关于js调用天气API的几种做法. 网上也能找到很多种方法,但是一般都是免费的不好用,好用的不免费. 以前用过新浪的天气api,查 ...
- 关于反射Assembly.Load("程序集").CreateInstance("命名空间.类")
关于反射Assembly.Load("程序集").CreateInstance("命名空间.类") 而不管在哪一层写这段代码其中的("程序集" ...
- 关于js的callback回调函数的理解
回调函数的处理逻辑理解:所谓的回调函数处理逻辑,其实就是先将回调函数的代码 冻结(或者理解为闲置),接着将这个回调函数的代码放到回调函数管理器的队列里面. 待回调函数被触发调用的时候,对应的回调函数的 ...
- PHPExcel导出excel
如果导出中文时出现乱码,可以尝试将字符串转换成gb2312,例如下面就把$yourStr从utf-8转换成了gb2312: $yourStr = mb_convert_encoding("g ...