hdu Keep In Touch

22 篇文章 0 订阅
7 篇文章 0 订阅

Keep In Touch

Accepts: 109
Submissions: 243
Time Limit: 4000/2000 MS (Java/Others)
Memory Limit: 262144/131072 K (Java/Others)
Problem Description

There are nnn cities numbered with successive integers from 111 to nnn in Byteland. Also, there are mmm one-way roads connecting the cities. The starting point of the iii-th road is uiu_iui while the ending point is viv_ivi.

There are 333 spies : 007, 008 and 009. They are going to start qqq secret missions.

During each mission, they may be in three different cities, and they contact each other using interphones. The radio frequency of theiii-th city is wiw_iwi. Two spies can contact with each other if and only if the absolute value of the difference of radio frequency between the cities where they are in is no more thanKKK. At each moment, three spies must choose a road and go to another city. The time for traveling each road is only a unit of time.

They can choose to end the mission in any city, even in the city where they started the mission. But they are not allowed to end mission in the middle of the roads. Now they want to know, for each mission whose start points are given, what's the number of possible ways, during which they can contact with each other at any moment when they are not on roads?

Two ways are considered different if and only if there exists at least one spy at different cities on the same moment.

Note : 333 spies must end the mission at the same time.

Input

The first line of the input contains an integer TTT(1≤T≤10)(1\leq T\leq 10)(1T10), denoting the number of test cases.

In each test case, the first line of the input contains four integers nnn(1≤n≤50),m(0≤m≤n(n−1)2),K(0≤K≤109),q(1≤q≤125000)(1\leq n\leq 50),m(0\leq m\leq \frac{n(n-1)}{2}),K(0\leq K\leq 10^9),q(1\leq q\leq 125000)(1n50),m(0m2n(n1)),K(0K109),q(1q125000), denoting the number of cities, the number of roads, the upper limit of interphone and the number of missions.

The second line of the input contains nnn integers w1,w2,...,wnw_1, w_2, ..., w_nw1,w2,...,wn(1≤wi≤109)(1\leq w_i\leq 10^9)(1wi109), denoting the radio frequency of the iii-th city.

Each of the next mmm lines contains two integers ui,viu_i,v_iui,vi(1≤ui<vi≤n)(1\leq u_i < v_i\leq n)(1ui<vin), denoting an one-way road. There are no multiple edges in the graph.

Each of the next qqq lines contains three integers x,y,z(1≤x,y,z≤n)x,y,z(1\leq x,y,z\leq n)x,y,z(1x,y,zn), denoting the starting point of the three spies in each mission. You can assume that there are at least one possible way in each mission.

Output

For each test case, print qqq lines with one integer per line. For each mission, print the number of possible ways modulo998244353998244353998244353.

Sample Input
1
4 4 2 10
8 8 4 1
1 3
1 4
2 3
2 4
1 1 1
1 1 2
1 2 1
1 2 2
2 1 1
2 1 2
2 2 1
2 2 2
3 3 3
4 4 4
Sample Output
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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值