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. 在win7-64bit环境下,boa-constructor 0.6.1 的palette面板中没有控件图标的解决方法

    在win7-64bit环境下,boa-constructor 0.6.1 的palette面板中没有控件图标,空白一片.将面板窗口拉大,发现那些图标在很下面的位置,X轴的排列与正常状态一致. 软件环境 ...

  2. Windows下配置Java开发环境

    学习Java第一步是配置本地开发环境,学习最基本的桌面开发,下面以win7为例配置Java开发环境,即:JDK+JRE+Eclipse,安装JDK的时候会默认安装JRE,根据提示安装就可以了. 首先去 ...

  3. ABAP 内表的行列转换-发货通知单-打印到Excel里-NEW

    *********************************************************************** * Title           : ZSDF002  ...

  4. Effective C++ -----条款19:设计class犹如设计type

    Class的设计就是type的设计.在定义一个新type之前,请确定你已经考虑过本条款覆盖的所有讨论主题. 新type的对象应该如何被创建和销毁? 对象的初始化和对象的赋值该有什么样的区别? 新typ ...

  5. Bestcoder13 1003.Find Sequence(hdu 5064) 解题报告

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5064 题目意思:给出n个数:a1, a2, ..., an,然后需要从中找出一个最长的序列 b1, b ...

  6. iOS MRC ARC 内存管理

    转自:http://www.jianshu.com/p/48665652e4e4 1. 什么是内存管理 程序在运行的过程中通常通过以下行为,来增加程序的的内存占用 创建一个OC对象 定义一个变量 调用 ...

  7. Hibernate基本CRUD

    1 hibernate 框架介绍 冬眠:开发的代码基本不变. 1.1回顾jdbc Java提供的标准的数据访问的API 作用:完成应用程序java程序中的数据和db中的数据进行交换. 工作过程: A ...

  8. Java容器题库

    一.    填空题 Java集合框架提供了一套性能优良.使用方便的接口和类,包括Collection和Map两大类,它们都位于  java.util  包中 队列和堆栈有些相似,不同之处在于栈是先进后 ...

  9. oracle sqlplus存储过程控制台输出信息

    如果你是使用PL/sql工具,在command 窗口下执行set serveroutput on 然后exec sp;可以看到了或者在sqlplus 中执行上面的代码

  10. 数的统计count(bzoj1036)

    Description 一棵树上有n个节点,编号分别为1到n,每个节点都有一个权值w.我们将以下面的形式来要求你对这棵树完成一些操作: I. CHANGE u t : 把结点u的权值改为t II. Q ...