Garvey reduced complexity
#pragmaComment (linker, "/stack:1024000000,1024000000")#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#include<vector>#include<map>#include<Set>#include<queue>#include<stack>#include<iostream>using namespaceStd;typedefLong LongLL;Const DoublePi=acos (-1.0), eps=1e-8;voidFile () {freopen ("D:\\in.txt","R", stdin); Freopen ("D:\\out.txt","W", stdout);} InlineintRead () {Charc = GetChar (); while(!isdigit (c)) C =GetChar (); intx =0; while(IsDigit (c)) {x = x *Ten+ C-'0'; c =GetChar ();} returnx;}Const intmaxn= -+Ten;intMod=998244353;intt,n,m,k,q,g[maxn][maxn],w[maxn],f[maxn][maxn][maxn][4];BOOLCheckintAintBintc) { if(ABS (W[a]-w[b]) >k)return 0; if(ABS (W[a]-w[c]) >k)return 0; if(ABS (W[b]-w[c]) >k)return 0; return 1;}intMain () {scanf ("%d", &t); while(t--) {scanf ("%d%d%d%d",&n,&m,&k,&q); Memset (g,0,sizeofg); for(intI=1; i<=n;i++) scanf ("%d",&W[i]); for(intI=1; i<=m;i++) { intU,v; scanf"%d%d",&u,&v); G[U][V]=1; } memset (F,0,sizeoff); for(inti=n;i>=1; i--) { for(intj=n;j>=1; j--) { for(intk=n;k>=1; k--) { if(check (i,j,k) = =0) f[i][j][k][0]=0; Elsef[i][j][k][0]= (f[i][j][k][0]+1)%MoD; for(ints=1; s<k;s++) if(G[s][k]) f[i][j][s][2]= (f[i][j][s][2]+f[i][j][k][0])%MoD; for(ints=1; s<=j;s++) if(G[s][j]) f[i][s][k][1]= (f[i][s][k][1]+f[i][j][k][2])%MoD; for(ints=1; s<=i;s++) if(G[s][i]) f[s][j][k][0]= (f[s][j][k][0]+f[i][j][k][1])%MoD; } } } for(intI=1; i<=q;i++) { intA,b,c; scanf"%d%d%d",&a,&b,&c); printf ("%d\n", f[a][b][c][0]); } } return 0;}
HDU 5807 Keep in Touch