标签:
Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 512000/512000 K (Java/Others)
Total Submission(s): 205 Accepted Submission(s): 32
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 }
综合(奇技淫巧):HDU 5118 GRE Words Once More!
标签:
原文地址:http://www.cnblogs.com/TenderRun/p/5929844.html