码迷,mamicode.com
首页 > 其他好文 > 详细

hdu Keep In Touch

时间:2016-08-06 21:58:49      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:

Keep In Touch

Accepts: 109
Submissions: 243
Time Limit: 4000/2000 MS (Java/Others)
Memory Limit: 262144/131072 K (Java/Others)
Problem Description

There are nnn cities numbered with successive integers from 111 to nnn in Byteland. Also, there are mmm one-way roads connecting the cities. The starting point of the iii-th road is uiu_iu?i?? while the ending point is viv_iv?i??.

There are 333 spies : 007, 008 and 009. They are going to start qqq secret missions.

During each mission, they may be in three different cities, and they contact each other using interphones. The radio frequency of the iii-th city is wiw_iw?i??. 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 KKK. 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 : 333 spies must end the mission at the same time.

Input

The first line of the input contains an integer TTT (1≤T≤10)(1\leq T\leq 10)(1T10), denoting the number of test cases.

In each test case, the first line of the input contains four integers nnn (1≤n≤50),m(0≤m≤n(n?1)2),K(0≤K≤109),q(1≤q≤125000)(1\leq n\leq 50),m(0\leq m\leq \frac{n(n-1)}{2}),K(0\leq K\leq 10^9),q(1\leq q\leq 125000)(1n50),m(0m?2??n(n?1)??),K(0K10?9??),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 nnn integers w1,w2,...,wnw_1, w_2, ..., w_nw?1??,w?2??,...,w?n?? (1≤wi≤109)(1\leq w_i\leq 10^9)(1w?i??10?9??), denoting the radio frequency of the iii-th city.

Each of the next mmm lines contains two integers ui,viu_i,v_iu?i??,v?i?? (1≤ui<vi≤n)(1\leq u_i < v_i\leq n)(1u?i??<v?i??n), denoting an one-way road. There are no multiple edges in the graph.

Each of the next qqq lines contains three integers x,y,z(1≤x,y,z≤n)x,y,z(1\leq x,y,z\leq n)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 qqq lines with one integer per line. For each mission, print the number of possible ways modulo 998244353998244353998244353.

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

一个无向图,有三个人,起点任意,但是三个人所在城市的频率差值不大于k  每次三人都必须移动到下一个点或者三人选择此时结束任务

问 对于起始点i j k  有几种结束方案

记忆化搜索  暴力枚举起点状态 


/************************************************
┆  ┏┓   ┏┓ ┆
┆┏┛┻━━━┛┻┓ ┆
┆┃       ┃ ┆
┆┃   ━   ┃ ┆
┆┃ ┳┛ ┗┳ ┃ ┆
┆┃       ┃ ┆
┆┃   ┻   ┃ ┆
┆┗━┓    ┏━┛ ┆
┆  ┃    ┃  ┆      
┆  ┃    ┗━━━┓ ┆
┆  ┃  AC代马   ┣┓┆
┆  ┃           ┏┛┆
┆  ┗┓┓┏━┳┓┏┛ ┆
┆   ┃┫┫ ┃┫┫ ┆
┆   ┗┻┛ ┗┻┛ ┆
************************************************ */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>
#include<iostream>
#include<queue>
#include<map>
#define ll long long
using namespace std;
ll sum[55][55][55];
int n,m,K,q;
bool Map[55][55];
int num[55],d[55];
const ll mod=998244353;

bool cheak(int i,int j,int l)
{
    if(num[i]-num[j]>K||num[j]-num[i]>K) return false ;
    if(num[i]-num[l]>K||num[l]-num[i]>K) return false ;
    if(num[l]-num[j]>K||num[j]-num[l]>K) return false ;
    return true ;
}

int Abs(int i)
{
    if(i<0) i=-i;
    return i;
}

ll dfs(int i,int j,int k)
{
    if(sum[i][j][k]>=0) return sum[i][j][k];
    ll ans=0;
    for(int ii=1; ii<=n; ii++)
    {
        if(Map[i][ii])
        {
            for(int jj=1; jj<=n; jj++)
            {
                if(Map[j][jj]&&Abs(num[ii]-num[jj])<=K)
                {
                    for(int l=1; l<=n; l++)
                    {
                        if(Map[k][l]&&cheak(ii,jj,l))
                        {
                            ans=(ans+dfs(ii,jj,l))%mod;
                        }
                    }
                }
            }
        }
    }
    sum[i][j][k]=(ans+1)%mod;
    return (ans+1)%mod;
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d%d",&n,&m,&K,&q);
        for(int i=1; i<=n; i++) scanf("%d",&num[i]);
        memset(sum,-1,sizeof(sum));
        memset(Map,false ,sizeof(Map));
        memset(d,0,sizeof(d));
        for(int i=0; i<m; i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            Map[u][v]=true;
            d[v]++;
        }
        for(int i=1; i<=n; i++)
            for(int j=1; j<=n; j++)
                if(Abs(num[i]-num[j])<=K)
                    for(int k=1; k<=n; k++)
                        if(cheak(i,j,k))
                            sum[i][j][k]=dfs(i,j,k);

        for(int i=0; i<q; i++)
        {
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            printf("%d\n",sum[a][b][c]);
        }
    }
    return 0;
}



hdu Keep In Touch

标签:

原文地址:http://blog.csdn.net/u013097262/article/details/52138463

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!