Keep In Touch
Total Submission(s): 328 Accepted Submission(s): 136
Problem Description
There are
n
cities numbered with successive integers from
There are
3
spies : 007, 008 and 009. They are going to start
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
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 :
Input
The first line of the input contains an integer
T
In each test case, the first line of the input contains four integers
n
The second line of the input contains
n
integers
Each of the next
Each of the next
q
lines contains three integers
Output
For each test case, print
q
lines with one integer per line. For each mission, print the number of possible ways modulo
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条路
u−v
保证
u<v
每个城市都有无线波段。
两个城市
u,v
间能通信的条件是
abs(wu−wv)≤K
q次询问,每次输入三个特工的出发城市,问在保持两两可通讯的前提下,有多少种行动方案。
一个合法的行动方案为:每一秒每一个特工都必须选择一条路走到下一个城市。三个特工可以在任意城市结束行动,甚至可以在起点结束,但要保证三人同时结束。
即找出三人各自的一条可行路线,并保证三条路线长度相同,且对应时刻所在城市两两可互相通讯。
=。=就是个搜索………………
比赛的时候脑抽了。。乱想一通……
dfs(a,b,c,typ)
表示第一个特工在a,第二个特工在b,第三个特工在c,轮到第typ个特工行动的方案数。
轮流走,每次第三个走完后判断一下是否可以互相通讯,可以返回一个价值,不可以返回0.然后dp数组存一下中间结果就不会爆炸了……
代码如下:
#include <iostream>
#include <cmath>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <list>
#include <algorithm>
#include <map>
#include <set>
#define LL long long
#define Pr pair<int,int>
#define fread(ch) freopen(ch,"r",stdin)
#define fwrite(ch) freopen(ch,"w",stdout)
using namespace std;
const int INF = 0x3f3f3f3f;
const int msz = 10000;
const int mod = 998244353;
const double eps = 1e-8;
int K;
int n;
bool mp[55][55];
int w[55];
LL dp[3][55][55][55];
bool cal(int u,int v)
{
return abs(w[u]-w[v]) <= K;
}
bool judge(int u,int v,int vv)
{
return cal(u,v) && cal(u,vv) && cal(v,vv);
}
LL dfs(int a[3],int typ)
{
if(dp[typ][a[0]][a[1]][a[2]] != -1) return dp[typ][a[0]][a[1]][a[2]];
LL ans = typ == 0;
for(int i = a[typ]+1; i <= n; ++i)
{
if(!mp[a[typ]][i]) continue;
if(typ == 2 && (!judge(a[(typ+1)%3],a[(typ+2)%3],i))) continue;
int c = a[typ];
a[typ] = i;
ans = (ans+dfs(a,(typ+1)%3))%mod;
a[typ] = c;
}
return dp[typ][a[0]][a[1]][a[2]] = ans;
}
int main()
{
//fread("");
//fwrite("");
int t,m,u,v;
int q;
scanf("%d",&t);
int uu[3];
while(t--)
{
scanf("%d%d%d%d",&n,&m,&K,&q);
for(int i = 1; i <= n; ++i) scanf("%d",&w[i]);
memset(mp,0,sizeof(mp));
while(m--)
{
scanf("%d%d",&u,&v);
mp[u][v] = 1;
}
memset(dp,-1,sizeof(dp));
while(q--)
{
scanf("%d%d%d",&uu[0],&uu[1],&uu[2]);
printf("%lld\n",dfs(uu,0));
}
}
return 0;
}