HDU 5807 Keep In Touch DP
Keep In Touch
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.
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.
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
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的更多相关文章
- HDU 5807 Keep In Touch
加维降复杂度 #pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #inc ...
- HDU 1003 Max Sum --- 经典DP
HDU 1003 相关链接 HDU 1231题解 题目大意:给定序列个数n及n个数,求该序列的最大连续子序列的和,要求输出最大连续子序列的和以及子序列的首位位置 解题思路:经典DP,可以定义 ...
- 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][ ...
- hdu 5094 Maze 状态压缩dp+广搜
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4092176.html 题目链接:hdu 5094 Maze 状态压缩dp+广搜 使用广度优先 ...
- hdu 2829 Lawrence(斜率优化DP)
题目链接:hdu 2829 Lawrence 题意: 在一条直线型的铁路上,每个站点有各自的权重num[i],每一段铁路(边)的权重(题目上说是战略价值什么的好像)是能经过这条边的所有站点的乘积之和. ...
- hdu 4568 Hunter 最短路+dp
Hunter Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- HDU 1231.最大连续子序列-dp+位置标记
最大连续子序列 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
- HDU 1078 FatMouse and Cheese ( DP, DFS)
HDU 1078 FatMouse and Cheese ( DP, DFS) 题目大意 给定一个 n * n 的矩阵, 矩阵的每个格子里都有一个值. 每次水平或垂直可以走 [1, k] 步, 从 ( ...
- HDOJ(HDU).1284 钱币兑换问题 (DP 完全背包)
HDOJ(HDU).1284 钱币兑换问题 (DP 完全背包) 题意分析 裸的完全背包问题 代码总览 #include <iostream> #include <cstdio> ...
随机推荐
- 在eclipse上开发hadoop2.5.2程序的快捷方法
本文仍然使用MapReduce的经典例子 WordCount来测试eclipse的开发环境. 与大部分教程不同的是,本文使用的hadoop是2.5.2的版本,相较于之前的0.X版本,hadoop 2. ...
- STL---总结
文章转自:http://www.cnblogs.com/biyeymyhjob/archive/2012/07/22/2603525.html 一.STL的六大组件 容器(Container),是一种 ...
- 基于MATLAB的离散小波变换
申明,本文非笔者原创,原文转载自: 基于Matlab的离散小波变换 http://blog.sina.com.cn/s/blog_725866260100ryh3.html 简介 在 ...
- mysql备份与还原
一.直接拷贝数据库文件 直接拷贝数据库文件一般是使用文件系统备份工具cp,适合小型数据库,是最可靠的. 当你拷贝数据库文件时,必须保证表没有正在使用.如果服务器在你拷贝一个表的时候改变这个表,拷贝就失 ...
- UIWebView内嵌网页 Xcode7.0以后的用法
UIWebView* webPage=[[UIWebView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight-64)]; ...
- Greedy:Physics Experiment(弹性碰撞模型)(POJ 3848)
物理实验 题目大意:有一个与地面垂直的管子,管口与地面相距H,管子里面有很多弹性球,从t=0时,第一个球从管口求开始下落,然后每1s就会又有球从球当前位置开始下落,球碰到地面原速返回,球与球之间相碰会 ...
- linux下QT Creator常见错误及解决办法
最近因为在做一个关于linux下计算机取证的小项目,需要写一个图形界面,所以想到了用QT来写,选用了linux下的集成开发环境QT Creator5.5.1,但刚刚安装好,竟然连一个"hel ...
- HDU 5752 Sqrt Bo (思维题) 2016杭电多校联合第三场
题目:传送门. 题意:一个很大的数n,最多开5次根号,问开几次根号可以得到1,如果5次还不能得到1就输出TAT. 题解:打表题,x1=1,x2=(x1+1)*(x1+1)-1,以此类推.x5是不超过l ...
- Debian8.3.0下安装Odoo8.0步骤
Debian8.3.0下安装Odoo8.0的方法 假设你已经安装好了Debian 系统,使用root帐号执行如下命令 # apt-get update && apt-get upgra ...
- OkHttp学习总结
This paper mainly includes the following contents okhttp ordinary operation. okhttp interceptors. Re ...