Test instructions
In Byteland there are altogether N cities, numbered 1 to N, with M-one-way roads connecting these cities,
Where the beginning of road I is UI??, the End is VI (1≤u?i?? <v?i?? ≤n). The spy team has 3 members:007,008, and 009, who are going to perform a Q-secret mission. In each mission, the trio may be in three different cities, and they will keep in touch with each other via walkie-talkie.
The radio frequency of the city numbered I is w?i??, if the absolute value of the radio frequency difference in two cities does not exceed K,
Then the radio can be switched on. Three agents each time must choose a road, go to the next city, each road will only need to spend 1 units of time. They can choose to terminate the task in any city, and even terminate the task at the beginning, but not on the road. Now they want to know,
For each task, given the starting position of three people, how many possible legal action plans make the action process any time in the city that they can 22 contact? Two scenarios are considered different when and only if there is at least one person at a time in the city different. Note: 3 agents must complete the task at the same time.
Ideas:
This question at first glance is O (n^6) of time complexity, because finally did not think out what good practice, wrote a pitch. Decisively be hack---
The positive solution is to transform three people at the same time into one and one dimension,
DP[I][J][L][P] said that three people in I,j,l, is now ready to go p This person's plan number, this is O (n^4) of the
/************************************************author:d evil*********************************************** * */#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<vector>#include<queue>#include<Set>#include<map>#include<string>#include<cmath>#include<stdlib.h>using namespaceStd;typedefLong LongLL;Const intinf=0x3f3f3f3f;Const intMod=998244353;Const intn=Wuyi;intdp[n][n][n][3],w[n];BOOLMp[n][n];intMain () {//freopen ("In.txt", "R", stdin); intT,n,m,k,q,x,y; scanf ("%d",&t); while(t--) {scanf ("%d%d%d%d",&n,&m,&k,&q); for(intI=1; i<=n;i++) scanf ("%d",&W[i]); Memset (MP,0,sizeof(MP)); while(m--) {scanf ("%d%d",&x,&y); Mp[x][y]=1; } memset (DP,0,sizeof(DP)); for(inti=n;i>=1; i--) { for(intj=n;j>=1; j--) { for(intl=n;l>=1; l--) { if(ABS (W[i]-w[j]) >k| | ABS (W[i]-w[l]) >k| | ABS (W[j]-w[l]) >k) dp[i][j][l][0]=0; Elsedp[i][j][l][0]= (dp[i][j][l][0]+1)%MoD; if(dp[i][j][l][0]) for(intp=1;p <l;p++) if(Mp[p][l]) dp[i][j][p][2]= (dp[i][j][p][2]+dp[i][j][l][0])%MoD; if(dp[i][j][l][2]) for(intp=1;p <j;p++) if(Mp[p][j]) dp[i][p][l][1]= (dp[i][p][l][1]+dp[i][j][l][2])%MoD; if(dp[i][j][l][1]) for(intp=1;p <i;p++) if(Mp[p][i]) dp[p][j][l][0]= (dp[p][j][l][0]+dp[i][j][l][1])%MoD; } } } while(q--) {scanf ("%d%d%d",&x,&y,&m); printf ("%d\n", dp[x][y][m][0]); } } return 0;}
HDU5807 Keep in Touch (segmented DP)