Found \ (K \) is small, it may set a \ (O (NK) \) a \ (the DP \) .
First, FIG built reverse run \ (the Dijkstra \) , obtains \ (dis [i] \) represents the \ (I \) to the \ (n-\) shortest distance.
It found shortest possible must satisfy is \ (D <= DIS <= D + K \) .
, Is a departure from a certain point of reverse thinking, you may consume \ (K \) surplus length units, final \ (n-\) .
Design \ (the DP \) state indicates:
\ (f [i] [j ] \) represents the \ (I \) to the \ (n-\) , and consumes \ (J \) Number Scheme surplus length units.
State transition:
Provided side \ ((U, V) \) , side length \ (W \)
则有 \(f[u][j] += f[v][j - (dis[v] + w - dis[u])]\)。
Boundary \ (F [n-] [0] =. 1 \) , the remainder being \ (0 \) .
Answer \ (\ sum_ I = {0} ^ {K} F [. 1] [I] \) .
Infinite solutions judgment:
Found to have infinite solutions if and only if:
There is a total right to \ (0 \) ring.
We can \ (DP \) to engage in a time \ (vis \) array judge, you do not need a separate sentence infinite understanding.
time complexity
The whole process implemented by the memory of the search, since a total of \ (NK \) states, each point enumerated \ (K \) times, that each edge overall enumerated \ (K \) times.
So the complexity of the \ (O (K (N + M)) \) .
\(Tips:\)
- And may move \ (n \) and then folded back, so $ u = $ n when not directly \ (return \)
- There may be after \ (n \) point \ (0 \) ring, so the \ (n \) when it points the way to judge a ring.
#include <cstdio>
#include <iostream>
#include <cstring>
#include <queue>
#include <vector>
#include <cstdlib>
using namespace std;
typedef pair<int, int> PII;
const int N = 100005, M = 400005, S = 51;
int n, m, K, P, f[N][S], dis[N];
int head[N], rhead[N], numE[2];
bool st[N], vis[N][S], ep = false;
struct E {
int next, v, w;
}e[M], r[M];
//建图
void inline add(E g[], int h[], int u, int v, int w, int p) {
g[++numE[p]] = (E) { h[u], v, w };
h[u] = numE[p];
}
//多组数据初始化
void inline init() {
ep = false;
memset(dis, 0x3f, sizeof dis);
memset(st, false, sizeof st);
numE[0] = numE[1] = 0;
memset(head, 0, sizeof head);
memset(rhead, 0, sizeof head);
memset(f, -1, sizeof f);
}
// Dijkstra 最短路
priority_queue<PII, vector<PII>, greater<PII> > q;
void inline dijkstra() {
dis[n] = 0; q.push(make_pair(0, n));
while(!q.empty()) {
PII u = q.top(); q.pop();
if(st[u.second]) continue;
st[u.second] = true;
for (int i = rhead[u.second]; i; i = r[i].next) {
int v = r[i].v;
if(dis[u.second] + r[i].w < dis[v]) {
dis[v] = dis[u.second] + r[i].w;
q.push(make_pair(dis[v], v));
}
}
}
}
//记忆化搜索
int dfs(int u, int j) {
if(vis[u][j]) { ep = true; return 0; }
if(f[u][j] != -1) return f[u][j];
vis[u][j] = true;
int &val = f[u][j] = 0;
for (int i = head[u]; i; i = e[i].next) {
int v = e[i].v, w = e[i].w;
//消耗的冗余长度折算
int k = j - (dis[v] + w - dis[u]);
if(0 <= k && k <= K) (val += dfs(v, k)) %= P;
if(ep) return 0;
}
vis[u][j] = false;
if(u == n && j == 0) val = 1;
return val;
}
int main() {
int T; scanf("%d", &T);
while(T--) {
init();
scanf("%d%d%d%d", &n, &m, &K, &P);
for (int i = 1, u, v, w; i <= m; i++) {
scanf("%d%d%d", &u, &v, &w);
add(e, head, u, v, w, 0); add(r, rhead, v, u, w, 1);
}
dijkstra();
int ans = 0;
for (int i = 0; i <= K; i++) {
memset(vis, false, sizeof vis);
(ans += dfs(1, i)) %= P;
if(ep) break;
}
if(ep) puts("-1");
else printf("%d\n", ans);
}
}