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 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 (1T10), denoting the number of test cases.

In each test case, the first line of the input contains four integers n (1n50),m(0mn(n1)2),K(0K109),q(1q125000), 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 (1wi109), denoting the radio frequency of the i-th city.

Each of the next m lines contains two integers ui,vi (1ui<vin), 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(1x,y,zn), 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
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
原创粉丝点击