【HDOJ 5807】Keep In Touch(记忆化dfs)

【HDOJ 5807】Keep In Touch(记忆化dfs)

Keep In Touch

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/131072 K (Java/Others)
Total Submission(s): 328    Accepted Submission(s): 136

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 (1/leT/le10), 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
1
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

Sample Output
3
3
3
3
3
3
3
3
1
1

Source
BestCoder Round #86

题目大意:
n点的城市,m条路 uv 保证 u<v
每个城市都有无线波段。
两个城市 u,v 间能通信的条件是 abs(wuwv)K
q次询问,每次输入三个特工的出发城市,问在保持两两可通讯的前提下,有多少种行动方案。
一个合法的行动方案为:每一秒每一个特工都必须选择一条路走到下一个城市。三个特工可以在任意城市结束行动,甚至可以在起点结束,但要保证三人同时结束。
即找出三人各自的一条可行路线,并保证三条路线长度相同,且对应时刻所在城市两两可互相通讯。

=。=就是个搜索………………
比赛的时候脑抽了。。乱想一通……

dfs(a,b,c,typ)
表示第一个特工在a,第二个特工在b,第三个特工在c,轮到第typ个特工行动的方案数。
轮流走,每次第三个走完后判断一下是否可以互相通讯,可以返回一个价值,不可以返回0.然后dp数组存一下中间结果就不会爆炸了……

代码如下:

#include <iostream>
#include <cmath>
#include <vector>
#include <cstdlib>
#include <cstdio>
#include <cstring>
#include <queue>
#include <stack>
#include <list>
#include <algorithm>
#include <map>
#include <set>
#define LL long long
#define Pr pair<int,int>
#define fread(ch) freopen(ch,"r",stdin)
#define fwrite(ch) freopen(ch,"w",stdout)

using namespace std;
const int INF = 0x3f3f3f3f;
const int msz = 10000;
const int mod = 998244353;
const double eps = 1e-8;

int K;
int n;
bool mp[55][55];
int w[55];
LL dp[3][55][55][55];

bool cal(int u,int v)
{
    return abs(w[u]-w[v]) <= K;
}

bool judge(int u,int v,int vv)
{
    return cal(u,v) && cal(u,vv) && cal(v,vv);
}

LL dfs(int a[3],int typ)
{
    if(dp[typ][a[0]][a[1]][a[2]] != -1) return dp[typ][a[0]][a[1]][a[2]];

    LL ans = typ == 0;

    for(int i = a[typ]+1; i <= n; ++i)
    {
        if(!mp[a[typ]][i]) continue;
        if(typ == 2 && (!judge(a[(typ+1)%3],a[(typ+2)%3],i))) continue;
        int c = a[typ];
        a[typ] = i;
        ans = (ans+dfs(a,(typ+1)%3))%mod;
        a[typ] = c;
    }

    return dp[typ][a[0]][a[1]][a[2]] = ans;
}

int main()
{
    //fread("");
    //fwrite("");

    int t,m,u,v;
    int q;

    scanf("%d",&t);
    int uu[3];

    while(t--)
    {
        scanf("%d%d%d%d",&n,&m,&K,&q);
        for(int i = 1; i <= n; ++i) scanf("%d",&w[i]);

        memset(mp,0,sizeof(mp));
        while(m--)
        {
            scanf("%d%d",&u,&v);
            mp[u][v] = 1;
        }

        memset(dp,-1,sizeof(dp));
        while(q--)
        {
            scanf("%d%d%d",&uu[0],&uu[1],&uu[2]);
            printf("%lld\n",dfs(uu,0));
        }
    }

    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值