hdu5807 Keep In Touch (加维优化)

 

Keep In Touch

 

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/131072 K (Java/Others)
Total Submission(s): 350    Accepted Submission(s): 145

 

 

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 (1≤T≤10), denoting the number of test cases.

In each test case, the first line of the input contains four integers n (1≤n≤50),m(0≤m≤n(n−1)2),K(0≤K≤109),q(1≤q≤125000), 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 (1≤wi≤109), denoting the radio frequency of the i-th city.

Each of the next m lines contains two integers ui,vi (1≤ui<vi≤n), 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(1≤x,y,z≤n), 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

 

 

Source

BestCoder Round #86

【题意】有n个城市,m条路,每个城市对应一个频率,当两个城市的频率差值的绝对值大于k时,两个城市就无法联系了。 有q个询问,每次给出三个人的起点位置,问你有多少种行动方案(要求每次三人都要行动,而且他们要能相互联系)。

 

【分析】暴力的方法,可以枚举起点然后可以达到的位置,但是这样的复杂度是O(n^6)。于是我们可以加维,用dp[1]表示第一个人走的方案数,dp[2]表示第二个人,dp[0]表示第三个人。 那么我们可以dp[0]->dp[1],dp[1]->dp[2],dp[2]->dp[0] 而且同时我们可以把复杂度降低到O(n^4)。

 

代码:

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<bitset>

#define F first
#define S second
#define mp make_pair
using namespace std;
typedef __int64 LL;
const int maxn = 3e5 + 10;
const int mod = 998244353;

vector <int> v[55];
int w[55];
LL dp[55][55][55][3];
int n,m,k,q;
int a[4];
bool judge(int x1,int x2,int x3)
{
    if(abs(w[x1]-w[x2])>k) return false;
    if(abs(w[x1]-w[x3])>k) return false;
    if(abs(w[x2]-w[x3])>k) return false;
    return true;
}

void solve()
{
    memset(dp,0,sizeof(dp));
    for(int x1=n; x1>=1; x1--)
    {
        for(int x2=n; x2>=1; x2--)
        {
            for(int x3=n; x3>=1; x3--)
            {
                if(judge(x1,x2,x3))
                dp[x1][x2][x3][0]++;
                for(int i=0; i<v[x1].size(); i++)
                {
                    dp[v[x1][i]][x2][x3][1]+=dp[x1][x2][x3][0];
                    dp[v[x1][i]][x2][x3][1]%=mod;
                }
                for(int i=0; i<v[x2].size(); i++)   
                {
                    if(abs(w[x1]-w[v[x2][i]])>k) continue;
                    dp[x1][v[x2][i]][x3][2]+=dp[x1][x2][x3][1];
                    dp[x1][v[x2][i]][x3][2]%=mod;
                }
                for(int i=0; i<v[x3].size(); i++)
                {
                    if(!judge(x1,x2,v[x3][i])) continue;
                    dp[x1][x2][v[x3][i]][0]+=dp[x1][x2][x3][2];
                    dp[x1][x2][v[x3][i]][0]%=mod;
                }
                if(!judge(x1,x2,x3)) dp[x1][x2][x3][0]=0;
            }
        }
    }
}

int main()
{
    int T,s,e;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d%d%d",&n,&m,&k,&q);
        for(int i=1; i<=n; i++)
            scanf("%d",&w[i]);
        for(int i=1; i<=n; i++)
            v[i].clear();
        for(int i=1; i<=m; i++)
            scanf("%d%d",&s,&e),v[e].push_back(s);
        solve();
        for(int i=1; i<=q; i++)
        {
            for(int j=0; j<3; j++)
                scanf("%d",&a[j]);
            printf("%I64d\n",dp[a[0]][a[1]][a[2]][0]);
        }
    }
    return 0;
}

 

 

 

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值