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][]);
}
}
}

HDU 5807 Keep In Touch DP的更多相关文章

  1. HDU 5807 Keep In Touch

    加维降复杂度 #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #inc ...

  2. HDU 1003 Max Sum --- 经典DP

    HDU 1003    相关链接   HDU 1231题解 题目大意:给定序列个数n及n个数,求该序列的最大连续子序列的和,要求输出最大连续子序列的和以及子序列的首位位置 解题思路:经典DP,可以定义 ...

  3. HDU5807 Keep In Touch DP

    // HDU5807 Keep In Touch DP // 思路:直接暴力是O(n^6).所以要优化一下 // dp[i][j][k][0]:当前点i j k的方案数 // dp[i][j][k][ ...

  4. hdu 5094 Maze 状态压缩dp+广搜

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092176.html 题目链接:hdu 5094 Maze 状态压缩dp+广搜 使用广度优先 ...

  5. hdu 2829 Lawrence(斜率优化DP)

    题目链接:hdu 2829 Lawrence 题意: 在一条直线型的铁路上,每个站点有各自的权重num[i],每一段铁路(边)的权重(题目上说是战略价值什么的好像)是能经过这条边的所有站点的乘积之和. ...

  6. hdu 4568 Hunter 最短路+dp

    Hunter Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  7. HDU 1231.最大连续子序列-dp+位置标记

    最大连续子序列 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Sub ...

  8. HDU 1078 FatMouse and Cheese ( DP, DFS)

    HDU 1078 FatMouse and Cheese ( DP, DFS) 题目大意 给定一个 n * n 的矩阵, 矩阵的每个格子里都有一个值. 每次水平或垂直可以走 [1, k] 步, 从 ( ...

  9. HDOJ(HDU).1284 钱币兑换问题 (DP 完全背包)

    HDOJ(HDU).1284 钱币兑换问题 (DP 完全背包) 题意分析 裸的完全背包问题 代码总览 #include <iostream> #include <cstdio> ...

随机推荐

  1. Apache2.4.6 添加虚拟主机

    apache2.4 与 apache2.2 的虚拟主机配置写法有所不同 apache2.2的写法: <VirtualHost *:80> ServerName domain.com Doc ...

  2. 使用Java中File类批量创建文件和批量修改文件名

    批量创建文件 int cont = 1; String s = "E:\\学习资料\\Java笔记-"; while(cont<100){ File f = new File ...

  3. best matched pair

    今天的模拟赛,被虐的不行....英文太差,弄不懂题意,弄懂题意了还不会... 感觉快要受不了了... #include <iostream> #include <cstdio> ...

  4. Android 启动白屏或者黑屏闪现解决

    1.设置Style //1.设置背景图Theme <style name="Theme.AppStartLoad" parent="android:Theme&qu ...

  5. 自定义ToolBar之一

    其实已经有很多大神写过这方面的文章了,不过我比较蠢吧,老有一些地方看不懂的,翻了很多关于Toolbar方面的文章和视频,这儿总结一下.  参考资料:youtube:slidenerd 阶段一 自定义配 ...

  6. PHP安全编程:不要让不相关的人看到报错信息

    没有不会犯错的开发者,PHP的错误报告功 能可以协助你确认和定位这些错误,可以提供的这些错误的详细描述,但如果被恶意攻击者看到,这就不妙了.不能让大众看到报错信息,这一点很重要.做到这一 点很容易,只 ...

  7. net 页面跳转

    前台: < a href="xx.html" target="_blank"> 后台: Response.Redirect("XXX.as ...

  8. OSG osgDB FileUtils FileNameUtil操作文件名相关函数

    /** Gets the parent path from full name (Ex: /a/b/c.Ext => /a/b). */extern OSGDB_EXPORT std::stri ...

  9. [Android] Android5.1系统自带的应用启动次数统计

    reference to : http://blog.csdn.net/elder_sword/article/details/50508257 前段时间要做一个统计手机中激活量的东东,这个统计不是单 ...

  10. August 22nd 2016 Week 35th Monday

    Have you ever given any thought to your future? 你有没有为将来打算过呢? Have you ever given any thought to your ...