HDU 5807 Keep In Touch

加维降复杂度

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-8;
void File()
{
    freopen("D:\\in.txt","r",stdin);
    freopen("D:\\out.txt","w",stdout);
}
inline int read()
{
    char c = getchar();  while(!isdigit(c)) c = getchar();
    int x = 0;
    while(isdigit(c)) { x = x * 10 + c - ‘0‘; c = getchar(); }
    return x;
}

const int maxn=50+10;
int mod=998244353;
int T,n,m,k,q,g[maxn][maxn],w[maxn],f[maxn][maxn][maxn][4];

bool check(int a,int b,int c)
{
    if(abs(w[a]-w[b])>k) return 0;
    if(abs(w[a]-w[c])>k) return 0;
    if(abs(w[b]-w[c])>k) return 0;
    return 1;
}

int main()
{
    scanf("%d",&T); while(T--)
    {
        scanf("%d%d%d%d",&n,&m,&k,&q);
        memset(g,0,sizeof g);
        for(int i=1;i<=n;i++) scanf("%d",&w[i]);
        for(int i=1;i<=m;i++)
        {
            int u,v; scanf("%d%d",&u,&v);
            g[u][v]=1;
        }
        memset(f,0,sizeof f);
        for(int i=n;i>=1;i--)
        {
            for(int j=n;j>=1;j--)
            {
                for(int k=n;k>=1;k--)
                {
                    if(check(i,j,k)==0) f[i][j][k][0]=0;
                    else f[i][j][k][0]=(f[i][j][k][0]+1)%mod;

                    for(int s=1;s<k;s++)
                        if(g[s][k]) f[i][j][s][2]=(f[i][j][s][2]+f[i][j][k][0])%mod;

                    for(int s=1;s<=j;s++)
                        if(g[s][j]) f[i][s][k][1]=(f[i][s][k][1]+f[i][j][k][2])%mod;

                    for(int s=1;s<=i;s++)
                        if(g[s][i]) f[s][j][k][0]=(f[s][j][k][0]+f[i][j][k][1])%mod;
                }
            }
        }

        for(int i=1;i<=q;i++)
        {
            int a,b,c; scanf("%d%d%d",&a,&b,&c);
            printf("%d\n",f[a][b][c][0]);
        }
    }
    return 0;
}
时间: 2024-08-23 04:03:56

HDU 5807 Keep In Touch的相关文章

HDU 5807 Keep In Touch DP

Keep In Touch 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

hdu 5807 分步dp

有三个人分别在三个位置,每一秒各走一步,三个城市可以联络的要求是两两权值差小于等于K.问有多少种不同的方案,让这三个人可以联络. dp[x][y][z]表示三个人分别在x,y,z时的答案,直接转移要花n^6,考虑每个人走一步是一种状态,即再加一维表示即将要走的人是哪一个,按照顺序转移0,1,2,0,1,2......所以就能转移.在dp[i][j][k][0]的时候三位步长一样.此时为答案,而如果这一个点是0说明这个状态本身就不能到,所以直接就是0,不可转移. #include <cstdio>

Hdu 5361 In Touch (dijkatrs+优先队列)

题目链接: Hdu 5361  In Touch 题目描述: 有n个传送机排成一排,编号从1到n,每个传送机都可以把自己位置的东西传送到距离自己[l, r]距离的位置,并且花费c,问从1号传送机到其他传送机的最小花费是多少?? 解题思路: 看了题解发现是dijkstra(求最短单源路径)+并查集(优化),刚开始的时候并不知道怎么用并查集优化,然后就提交了一个没有并查集的代码,果断TLE.然后开始搜代码,突然惊叹真是优化的太美妙. 因为每次从队列中取出的都是最优的,所以更新dis的时候可以直接把这

hdu Keep In Touch

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

HDU 5361 In Touch (使用SET集合)

In Touch Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 1184    Accepted Submission(s): 313 Problem Description There are n soda living in a straight line. soda are numbered by1,2,-,n from l

HDU 5361 In Touch (2015 多校6 1009)

题目:http://acm.hdu.edu.cn/showproblem.php?pid=5361 题意:最短路,求源点到所有点的最短距离.但与普通最短路不同的是,给出的边是某点到区间[l,r]内任意点的距离. 输入一个n,代表n个点,输入n个l[i],输入n个r[i],输入n个c[i]. 对于i,表示i到区间[i - r[i]],i - l[i]]和区间[i + l[i],i + r[i]]内的任意点的距离为c[i]. 求1到各个点的最短距离. 思路:若建边跑最短路的话,因为边过多,所以不可行

In Touch (hdu 5361 优先队列的Dij + 并查集优化)

In Touch Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 1109    Accepted Submission(s): 298 Problem Description There are n soda living in a straight line. soda are numbered by 1,2,-,n from

HDU 3207 Ikki&#39;s Story IV - Panda&#39;s Trick(图论-2SAT,图论-tarjan)

Ikki's Story IV - Panda's Trick Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 7821   Accepted: 2892 Description liympanda, one of Ikki's friend, likes playing games with Ikki. Today after minesweeping with Ikki and winning so many tim

HDU 5515 Game of Flying Circus 二分

Game of Flying Circus Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5515 Description The discovery of anti-gravitations technology changed the world. The invention of anti-gravitation shoes (Grav-shoes) enable