Keep In Touch
Accepts: 109
Submissions: 243
Time Limit: 4000/2000 MS (Java/Others)
Memory Limit: 262144/131072 K (Java/Others)
3 3 3 3 3 3 3 3 1 1
贴个大神的代码
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<functional>
using namespace std;
#define LL long long
const int maxn = 55;
const int mod = 998244353;
int w[maxn],k;
bool jud(int x,int y){
return abs(w[x] - w[y])<=k;
}
bool jud(int x,int y,int z){
return jud(x,y) && jud(y,z) && jud(z,x);
}
bool Map[maxn][maxn];
int dp[maxn][maxn][maxn][3];
int n;
int dfs(int x,int y,int z,int t){
int & ndp = dp[x][y][z][t];
if(ndp != -1) return ndp;
ndp = 0;
if(t==0 && jud(x,y,z)) ndp = 1;
for(int i=1;i<=n;i++){
int tp = 0;
if(t==0 && Map[x][i]) tp = dfs(i,y,z,1);
else if(t==1 && Map[y][i]) tp = dfs(x,i,z,2);
else if(t==2 && Map[z][i] && jud(x,y,i)) tp = dfs(x,y,i,0);
(ndp += tp) %= mod;
}
return ndp;
}
int main(){
int T;
scanf("%d",&T);
int m,q;
while(T-- && ~scanf("%d %d %d %d",&n,&m,&k,&q)){
int x,y,z;
for(int i=1;i<=n;i++){
scanf("%d",&w[i]);
}
memset(Map,0,sizeof(Map));
while(m--){
scanf("%d %d",&x,&y);
Map[x][y] = true;
}
memset(dp,-1,sizeof(dp));
while(q--){
scanf("%d %d %d",&x,&y,&z);
printf("%d\n",dfs(x,y,z,0));
}
}
return 0;
}