Keep In Touch
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/131072 K (Java/Others)
Total Submission(s): 324 Accepted Submission(s): 134
Total Submission(s): 324 Accepted Submission(s): 134
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.
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.
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
大体题意:
整体思路先得求出任意三个位置作为起始坐标的答案,因为输入时保证w < v 所以位置肯定是从起始位置向n,n,n终点移动的,所以我们要倒着转移dp。
dp[i][j][k][0]是0的话,那就表示无法转移了,下面虽然加了,但是和没加一个样!相当于没有转移!
转移的话,就是分布转移,先是第三个人标号为2的人先走,走完后下一个人加上他的dp,依次类推!
详细见代码:
大体题意:
在Byteland一共有nnn个城市,编号依次为111到nnn,同时有mmm条单向道路连接着这些城市, 其中第iii条道路的起点为uiu_iui,终点为vi(1≤ui<vi≤n)v_i(1\leq u_i < v_i\leq n)vi(1≤ui<vi≤n)。 特工团队一共有333名成员:007,008,以及009,他们将要执行qqq次秘密任务。 在每次任务中,三人可能会处于三个不同的城市,他们互相之间通过对讲机保持联络。 编号为iii的城市的无线电频为wiw_iwi,如果两个城市的无线电频差值的绝对值不超过KKK,那么无线电就可以接通。 三个特工每个时刻必须要选择一条道路,走到下一个城市,每条道路都只需要花费111单位时间。 他们可以选择在任意城市终止任务,甚至可以在起点就终止任务,但不允许在道路上终止任务。 现在他们想知道,对于每次任务,给定三个人的起始位置,有多少种可能的合法行动方案, 使得行动过程中任意在城市的时刻,他们都可以两两联络? 两个方案被视作不同当且仅当至少存在一个人在某一时刻所在的城市不同。 注意:333个特工必须同时结束任务。官方思路:
考虑dp,设f[i][j][k]f[i][j][k]f[i][j][k]表示三个人分别在i,j,ki,j,ki,j,k时的方案数,直接转移是O(n6)O(n^6)O(n6)的。
于是考虑加维,设f[i][j][k][now]f[i][j][k][now]f[i][j][k][now]表示三个人分别在i,j,ki,j,ki,j,k时,目前准备走nownownow这个人的方案数,那么转移复杂度就降低到了O(n4)O(n^4)O(n4)。
整体思路先得求出任意三个位置作为起始坐标的答案,因为输入时保证w < v 所以位置肯定是从起始位置向n,n,n终点移动的,所以我们要倒着转移dp。
dp[i][j][k][0]是0的话,那就表示无法转移了,下面虽然加了,但是和没加一个样!相当于没有转移!
转移的话,就是分布转移,先是第三个人标号为2的人先走,走完后下一个人加上他的dp,依次类推!
详细见代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn = 50 + 5;
int dp[maxn][maxn][maxn][3];
vector<int>g[maxn];
int n,m,K,q;
int w[maxn];
const int mod = 998244353;
bool judge(int i,int j,int k){
return abs(w[i]-w[j]) <= K && abs(w[i]-w[k]) <= K && abs(w[j]-w[k]) <= K;
}
int main(){
int T;
scanf("%d",&T);
while(T--){
for (int i = 0; i < maxn; ++i)g[i].clear();
memset(dp,0,sizeof dp);
scanf("%d %d %d %d",&n,&m,&K,&q);
for (int i = 1 ; i <= n ;++i)scanf("%d",&w[i]);
for (int i = 0 ; i < m; ++i){
int u,v;
scanf("%d %d",&u,&v);
g[v].push_back(u);
}
for (int i = n; i >= 1; --i){
for (int j = n; j >= 1; --j){
for (int k = n; k >= 1; --k){
if (!judge(i,j,k)) dp[i][j][k][0] = 0;
else dp[i][j][k][0] = (dp[i][j][k][0] + 1) % mod;
int lenk = g[k].size();
for (int l = 0; l < lenk ;++l){
int x = g[k][l];
dp[i][j][x][2] = (dp[i][j][x][2] % mod + dp[i][j][k][0] % mod) % mod;
}
int lenj = g[j].size();
for (int l = 0; l < lenj ;++l){
int x = g[j][l];
dp[i][x][k][1] = (dp[i][x][k][1] % mod + dp[i][j][k][2] % mod) % mod;
}
int leni = g[i].size();
for (int l = 0; l < leni ;++l){
int x = g[i][l];
dp[x][j][k][0] = (dp[x][j][k][0] % mod + dp[i][j][k][1] % mod) % mod;
}
}
}
}
for (int i = 0; i < q; ++i){
int x,y,z;
scanf("%d %d %d",&x,&y,&z);
printf("%d\n",dp[x][y][z][0]);
}
}
return 0;
}