博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
综合(奇技淫巧):HDU 5118 GRE Words Once More!
阅读量:4970 次
发布时间:2019-06-12

本文共 3507 字,大约阅读时间需要 11 分钟。

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 ≤ 10
5, 0 ≤ M ≤ 10
5, 1 ≤ Q ≤ 10
5).
The second line contains N - 1 integers s
2, . . . , s
n. If the i-th vertex is special, then s
i = 1. Otherwise, s
i = 0. Vertex 1 is never special.
Each of the following M lines contains three integers a
i, b
i, c
i denoting an edge from vertex ai to vertex b
i labeled with c
i (1 ≤ a
i, b
i ≤ N, 1 ≤ c
i ≤ 10
9). For each vertex v, all outgoing edges are labeled with distinct integers.
Each of the following Q lines contains the integer ki (1 ≤ k
i ≤ 10
8) 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 
2 #include
3 #include
4 #include
5 #include
6 using namespace std; 7 const int N=200010,M=100000000; 8 vector
>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 }

 

转载于:https://www.cnblogs.com/TenderRun/p/5929844.html

你可能感兴趣的文章
SQLite使用EF6的连接配置
查看>>
学习ASP.NET Core Razor 编程系列十二——在页面中增加校验
查看>>
虚拟机连不上网
查看>>
最近一月研报推荐次数最多的最热股票
查看>>
JavaScript rowIndex和cellIndex
查看>>
判断滑动方向UITableView
查看>>
UILocalNotification的使用——本地通知
查看>>
数据结构与算法学习
查看>>
S2SH 商城系统(2)——项目框架搭建
查看>>
poj2389-Bull Math(大整数乘法)
查看>>
项目管理过程如何管理不合作用户
查看>>
6大设计原则
查看>>
java一般要点
查看>>
iOS开源加密相册Agony的实现(七)
查看>>
substr,mb_substr,iconv_substr,array_slice
查看>>
调皮的转义之addslashes
查看>>
灰度图像的直方图
查看>>
Pro/Toolkit示例之一:异步启动ProE
查看>>
Dapper.NET——轻量ORM
查看>>
CodeForces - 992D Nastya and a Game
查看>>