HDU 5807 Keep In Touch

Problem Description
There are  n  cities numbered with successive integers from  1  to  n  in Byteland. Also, there are  m  one-way roads connecting the cities. The starting point of the  i -th road is  ui  while the ending point is  vi .

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

During each mission, they may be in three different cities, and they contact each other using interphones. The radio frequency of the  i -th city is  wi . 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 than  K . 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 :  3  spies must end the mission at the same time.
 

Input
The first line of the input contains an integer  T   (1T10) , denoting the number of test cases.

In each test case, the first line of the input contains four integers  n   (1n50),m(0mn(n1)2),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  n  integers  w1,w2,...,wn   (1wi109) , denoting the radio frequency of the  i -th city.

Each of the next  m  lines contains two integers  ui,vi   (1ui<vin) , denoting an one-way road. There are no multiple edges in the graph.

Each of the next  q  lines contains three integers  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  q  lines with one integer per line. For each mission, print the number of possible ways modulo  998244353 .
 

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

一次性转移三个人要n^6,所以考虑一个一个的走,就是加上一维,判断现在是谁走,然后在最初的时候判断是否可行。

#include<set>
#include<map>
#include<ctime>
#include<cmath>
#include<stack>
#include<queue>
#include<bitset>
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
#define rep(i,j,k) for (int i = j; i <= k; i++)
#define per(i,j,k) for (int i = j; i >= k; i--)
using namespace std;
typedef __int64 LL;
const int low(int x) { return x&-x; }
const double eps = 1e-8;
const int INF = 0x7FFFFFFF;
const int mod = 998244353;
const int N = 55;
int T, n, m, K, q, x, y, z;
int a[N], f[N][N][N][3], mp[N][N], d[N][N];

int main()
{
    scanf("%d", &T);
    while (T--)
    {
        scanf("%d%d%d%d", &n, &m, &K, &q);
        rep(i, 1, n) scanf("%d", &a[i]);
        rep(i, 1, n) rep(j, 1, n)
        {
            mp[i][j] = abs(a[i] - a[j]) <= K;
            d[i][j] = 0;
        }

        while (m--)
        {
            scanf("%d%d", &x, &y);
            d[x][y] = 1;
        }

        per(i, n, 1) per(j, n, 1) per(k, n, 1)
        {
            f[i][j][k][1] = f[i][j][k][2] = 0;
            rep(w, j + 1, n) (f[i][j][k][1] += d[j][w] * f[i][w][k][2]) %= mod;
            rep(w, k + 1, n) (f[i][j][k][2] += d[k][w] * f[i][j][w][0]) %= mod;
            if (!(f[i][j][k][0] = mp[i][j] && mp[i][k] && mp[j][k])) continue;
            rep(w, i + 1, n) (f[i][j][k][0] += d[i][w] * f[w][j][k][1]) %= mod;
        }

        while (q--)
        {
            scanf("%d%d%d", &x, &y, &z);
            printf("%d\n", f[x][y][z][0]);
        }
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值