HDU 5807 Keep In Touch DP

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 (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
 

题意:

  BestCoder Round #86 1004 中文题面

题解

  首先dp[i][j][k] a,b,c分别在i,j,k三个点得答案

  这样暴力DP 在完全图下复杂度 O(N^6)

  于是考虑加维,设f[i][j][k][now]

   f[i][j][k][now]表示三个人分别在i,j,k时,目前准备走now这个人的方案数,那么转移复杂度就降低到了O(n^4)

  

#include<bits/stdc++.h>
using namespace std; #pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair typedef long long LL;
const long long INF = 1e18;
const double Pi = acos(-1.0);
const int N = +, M = 5e5+, inf = 2e9, mod = ; LL dp[N][N][N][];
vector<int > G[N];
int n,m,K,q,w[N],T;
void solve() {
for(int i = n; i >= ; --i)
for(int j = n; j >= ; --j) {
for(int k = n; k >= ; --k) {
dp[i][j][k][] = ;//dp[i][j][k][1] = dp[i][j][k][2] = 0;
for(int ii = ; ii < G[i].size(); ++ii) dp[i][j][k][] = (dp[G[i][ii]][j][k][] + dp[i][j][k][]) % mod;
for(int ii = ; ii < G[j].size(); ++ii) dp[i][j][k][] = (dp[i][G[j][ii]][k][] + dp[i][j][k][]) % mod;
for(int ii = ; ii < G[k].size(); ++ii) dp[i][j][k][] = (dp[i][j][G[k][ii]][] + dp[i][j][k][]) % mod;
if(abs(w[i] - w[j]) > K || abs(w[i] - w[k]) > K || abs(w[k] - w[j]) > K) dp[i][j][k][] = ;
}
}
}
int main () {
scanf("%d",&T);
while(T--) {
scanf("%d%d%d%d",&n,&m,&K,&q);
for(int i = ; i <= n; ++i) scanf("%d",&w[i]);
for(int i = ; i < N; ++i) G[i].clear();
for(int i = ; i <= m; ++i) {
int u,v;
scanf("%d%d",&u,&v);
G[u].push_back(v);
}
memset(dp,,sizeof(dp));
solve();
while(q--) {
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
printf("%I64d\n",dp[a][b][c][]);
}
}
}
上一篇:CF526F Pudding Monsters


下一篇:Qt编写安防视频监控系统37-onvif预置位