hdu5807 Keep In Touch (加维优化)
来源:互联网 发布:vs code php 常用插件 编辑:程序博客网 时间:2024/05/29 19:25
Keep In Touch
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/131072 K (Java/Others)Total Submission(s): 350 Accepted Submission(s): 145
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 are3 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 thei -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.
There are
During each mission, they may be in three different cities, and they contact each other using interphones. The radio frequency of the
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 :
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 integersn (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 containsn integers w1,w2,...,wn (1≤wi≤109) , denoting the radio frequency of the i -th city.
Each of the nextm 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 nextq 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.
In each test case, the first line of the input contains four integers
The second line of the input contains
Each of the next
Each of the next
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
14 4 2 108 8 4 11 31 42 32 41 1 11 1 21 2 11 2 22 1 12 1 22 2 12 2 23 3 34 4 4
Sample Output
3333333311
Source
BestCoder Round #86
【题意】有n个城市,m条路,每个城市对应一个频率,当两个城市的频率差值的绝对值大于k时,两个城市就无法联系了。 有q个询问,每次给出三个人的起点位置,问你有多少种行动方案(要求每次三人都要行动,而且他们要能相互联系)。【分析】暴力的方法,可以枚举起点然后可以达到的位置,但是这样的复杂度是O(n^6)。于是我们可以加维,用dp[1]表示第一个人走的方案数,dp[2]表示第二个人,dp[0]表示第三个人。 那么我们可以dp[0]->dp[1],dp[1]->dp[2],dp[2]->dp[0] 而且同时我们可以把复杂度降低到O(n^4)。
代码:
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<map>#include<set>#include<vector>#include<bitset>#define F first#define S second#define mp make_pairusing namespace std;typedef __int64 LL;const int maxn = 3e5 + 10;const int mod = 998244353;vector <int> v[55];int w[55];LL dp[55][55][55][3];int n,m,k,q;int a[4];bool judge(int x1,int x2,int x3){ if(abs(w[x1]-w[x2])>k) return false; if(abs(w[x1]-w[x3])>k) return false; if(abs(w[x2]-w[x3])>k) return false; return true;}void solve(){ memset(dp,0,sizeof(dp)); for(int x1=n; x1>=1; x1--) { for(int x2=n; x2>=1; x2--) { for(int x3=n; x3>=1; x3--) { if(judge(x1,x2,x3)) dp[x1][x2][x3][0]++; for(int i=0; i<v[x1].size(); i++) { dp[v[x1][i]][x2][x3][1]+=dp[x1][x2][x3][0]; dp[v[x1][i]][x2][x3][1]%=mod; } for(int i=0; i<v[x2].size(); i++) { if(abs(w[x1]-w[v[x2][i]])>k) continue; dp[x1][v[x2][i]][x3][2]+=dp[x1][x2][x3][1]; dp[x1][v[x2][i]][x3][2]%=mod; } for(int i=0; i<v[x3].size(); i++) { if(!judge(x1,x2,v[x3][i])) continue; dp[x1][x2][v[x3][i]][0]+=dp[x1][x2][x3][2]; dp[x1][x2][v[x3][i]][0]%=mod; } if(!judge(x1,x2,x3)) dp[x1][x2][x3][0]=0; } } }}int main(){ int T,s,e; scanf("%d",&T); while(T--) { scanf("%d%d%d%d",&n,&m,&k,&q); for(int i=1; i<=n; i++) scanf("%d",&w[i]); for(int i=1; i<=n; i++) v[i].clear(); for(int i=1; i<=m; i++) scanf("%d%d",&s,&e),v[e].push_back(s); solve(); for(int i=1; i<=q; i++) { for(int j=0; j<3; j++) scanf("%d",&a[j]); printf("%I64d\n",dp[a[0]][a[1]][a[2]][0]); } } return 0;}
0 0
- hdu5807 Keep In Touch (加维优化)
- Keep In Touch (dp 优化)
- 【HDU5807 BestCoder Round 86D】【分步式DP 状态转化】Keep In Touch 三人起点(i,j,k)保持联络条件下行走路径数
- HDU 5807 Keep In Touch(dp)
- hdu Keep In Touch
- HDOJ 5807 Keep In Touch
- hdu 5807 Keep In Touch
- HDU 5807 Keep In Touch
- hdoj 5807 Keep In Touch
- HDU-5807 Keep In Touch
- HDU 5807 Keep In Touch (分步DP)
- 【HDOJ 5807】Keep In Touch(记忆化dfs)
- HDU 5807 Keep In Touch dp
- HDU 5807 BestCoder Round #86 Keep In Touch (分段式DP)
- RO1-Keep in touch with the family.doc
- HDU 5807/BC 86D Keep In Touch[时间换空间]
- In Touch (hdu 5361 优先队列的Dij + 并查集优化)
- 线段树优化连边,Dijkstra(In Touch,HDU 5361)
- 单Activity多Fragment模式快速构建一个App
- 学习写博客
- spring文件无法解析占位符,按照字符串解析
- 第三天运算符以及window常用快捷键
- iOS开发中NSLog的替换
- hdu5807 Keep In Touch (加维优化)
- VS2010 MFC编辑框如何改变字体的大小和颜色
- boost之bind的使用
- spring中返回json数据时出现乱码
- spring实战 — spring数据库事务
- View事件分发
- 搭建ES脚手架相关问题
- 每天一题 leetcode-8 String to Integer (atoi)
- TCP长连接与短连接的区别