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

HDU 5807 Keep In Touch DP

时间:2016-08-12 18:17:42      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:

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 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
 
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
 

题意:

  BestCoder Round #86 1004 中文题面

题解

  首先dp[i][j][k] a,b,c分别在i,j,k三个点得答案

  这样暴力DP 在完全图下复杂度 O(N^6)

  于是考虑加维,设f[i][j][k][now]

   f[i][j][k][now]表示三个人分别在i,j,k时,目前准备走now这个人的方案数,那么转移复杂度就降低到了O(n^4)

 

  

#include<bits/stdc++.h>
using namespace std;

#pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair

typedef long long LL;
const long long INF = 1e18;
const double Pi = acos(-1.0);
const int N = 50+10, M = 5e5+11, inf = 2e9, mod = 998244353;

LL dp[N][N][N][3];
vector<int > G[N];
int n,m,K,q,w[N],T;
void solve() {
        for(int i = n; i >= 1; --i)
        for(int j = n; j >= 1; --j) {
            for(int k = n; k >= 1; --k) {
                dp[i][j][k][0] = 1;//dp[i][j][k][1] = dp[i][j][k][2] = 0;
                for(int ii = 0; ii < G[i].size(); ++ii) dp[i][j][k][0] = (dp[G[i][ii]][j][k][2] + dp[i][j][k][0]) % mod;
                for(int ii = 0; ii < G[j].size(); ++ii) dp[i][j][k][1] = (dp[i][G[j][ii]][k][0] + dp[i][j][k][1]) % mod;
                for(int ii = 0; ii < G[k].size(); ++ii) dp[i][j][k][2] = (dp[i][j][G[k][ii]][1] + dp[i][j][k][2]) % mod;
                if(abs(w[i] - w[j]) > K || abs(w[i] - w[k]) > K || abs(w[k] - w[j]) > K) dp[i][j][k][0] = 0;
            }
        }
}
int main () {
        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 = 0; i < N; ++i) G[i].clear();
            for(int i = 1; i <= m; ++i) {
                    int u,v;
                    scanf("%d%d",&u,&v);
                    G[u].push_back(v);
            }
            memset(dp,0,sizeof(dp));
            solve();
            while(q--) {
                    int a,b,c;
                    scanf("%d%d%d",&a,&b,&c);
                    printf("%I64d\n",dp[a][b][c][0]);
            }
        }
}

 

HDU 5807 Keep In Touch DP

标签:

原文地址:http://www.cnblogs.com/zxhl/p/5765844.html

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