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> ...
随机推荐
- 【leetcode】Text Justification
Text Justification Given an array of words and a length L, format the text such that each line has e ...
- Window 下 Qt5 使用QMediaplayer 进行视频播放 流播放问题
int main(int argc, char *argv[]) { QApplication a(argc, argv); QWidget *widget = new QWidget; widget ...
- ansible操作远程服务器报Error: ansible requires the stdlib json or simplejson module, neither was found!
通过ansible执行远程命令时报如下异常: Error: ansible requires the stdlib json or simplejson module, neither was fou ...
- C++实现VPN工具之代码示例
创建.连接.挂断.删除VPN实现起来并不难,下面给出一套比较完整的代码.该段代码只是示例代码,但是已经通过了编译,对API的使用和VPN操作步骤是没问题的.具体每个API代表的意义可以参看<C+ ...
- Core Data 多线程时多个context使用
原文地址:http://blog.csdn.net/willmomo/article/details/19759413 写的非常好!我这里截取一点重要的,备份,请去原地址阅读! 本人去年10月份才接触 ...
- iOS 串行网络请求。。。待研究
nsurlsession 和 nsurlconnection 能实现吗? 手动实现的关键点在哪里? 我这里说的串行网络请求,指的是第一个网络请求不返回数据,第二个网络请求就不能开始. AFNetwor ...
- iOS 在UITableViewCell中加入自定义view时view的frame设定注意
由于需要重用同一个布局,于是在cellForRowAtIndexPath中把自定义view加在了cell上,我是这样设定view的frame的 var screenFrame = UIScreen.m ...
- ubuntu14.04和win7共享文件夹
环境:vmware12 问题:安装了vmware-tools,但是在/mnt/hgfs下面看不到共享的文件夹. 按照网上的一些经验和教程使用如下命令: mount -t vmhgfs .host:/ ...
- SAP SMARTFORM 变量显示技巧
&symbol& (括号中,小写字母为变量) &symbol& 屏蔽从第一位开始的N位&symbol (n)& 只显示前N位&sym ...
- imageNamed和imageWithContentsOfFile-无法加载图片的问题
问题描述 图片资源放在Assets.xcassets中,分别用UIImage的类方法imageNamed和imageWithContentsOfFile获取图片对象,但发生奇怪的情况,前者获取到图片对 ...