//@winlere
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std; typedef long long ll;
inline int qr(){
register int ret=0,f=0;
register char c=getchar();
while(c<48||c>57)f|=c==45,c=getchar();
while(c>=48&&c<=57) ret=ret*10+c-48,c=getchar();
return f?-ret:ret;
}
const int maxn=1e5+5;
template<class M>
struct HEAP{
M data[maxn*2];
int cnt;
inline void down(const int&pos){
for(int t=pos,k;(t<<1)<=cnt;t=k){
k=t<<1;
if(k<cnt&&data[k|1]<data[k]) k|=1;
if(data[t]>data[k]) swap(data[t],data[k]);
else return;
}
}
inline void up(const int&pos){
for(int t=pos;t>>1;t>>=1)
if(data[t]<data[t>>1]) swap(data[t],data[t>>1]);
else return;
}
inline void push(const M&x){data[++cnt]=x,up(cnt);}
inline void pop(){swap(data[1],data[cnt--]);down(1);}
inline M top(){return data[1];}
inline int size(){return cnt;}
};
HEAP< pair<int,int> > q;
struct E{
int to,nx,w;
E(){to=nx=w=0;}
E(const int&x,const int&y,const int&z){to=x; nx=y; w=z;}
}e[maxn<<2];
int head[maxn],cnt,head0[maxn];
inline void add(const int&fr,const int&to,const int&w,int*h=head){e[++cnt]=E(to,h[fr],w),h[fr]=cnt;}
int d[maxn],n,m,k,mod;
typedef pair<int,int> P;
const int inf=1e9;
inline void dij(){
for(int t=1;t<=n;++t) d[t]=inf;
q.push((P){d[n]=0,n});
while(q.size()){
P now=q.top(); q.pop();
if(now.first>d[now.second]) continue;
for(int t=head[now.second];t;t=e[t].nx)
if(d[e[t].to]>d[now.second]+e[t].w)
q.push((P){d[e[t].to]=d[now.second]+e[t].w,e[t].to});
}
}
int dp[55][maxn];
bool usd[55][maxn];
bool in[55][maxn];
int dfs(const int&now,const int&k){
if(in[k][now])return -1;
if(usd[k][now]) return dp[k][now];
dp[k][now]=now==n;
in[k][now]=usd[k][now]=1;
for(int t=head0[now];t;t=e[t].nx){
int g=e[t].w-(d[now]-d[e[t].to]),ret;
if(g>k)continue;
if(ret=dfs(e[t].to,k-g),-1==ret) return dp[k][now]=-1;
dp[k][now]=(dp[k][now]+ret)%mod;
}
in[k][now]=0;
return dp[k][now];
}
int main(){
int T=qr();
while(T--){
cnt=0;
n=qr(); m=qr(); k=qr(); mod=qr();
for(register int t=0;t<=n;++t) head[t]=head0[t]=0;
for(int i=0;i<=k;++i)
for(register int t=0;t<=n;++t)
dp[i][t]=usd[i][t]=in[i][t]=0;
for(int t=1,t1,t2,t3;t<=m;++t)
t1=qr(),t2=qr(),t3=qr(),add(t2,t1,t3),add(t1,t2,t3,head0);
dij();
//for(int t=1;t<=n;++t) printf("%d\n",d[t]);
printf("%d\n",dfs(1,k));
}
return 0;
}
Compilation | N/A | N/A | Compile OK | Score: N/A | 显示更多 |
Testcase #1 | 889.36 us | 6 MB + 168 KB | Accepted | Score: 10 | 显示更多 |
Testcase #2 | 1.146 ms | 6 MB + 168 KB | Accepted | Score: 10 | 显示更多 |
Testcase #3 | 5.415 ms | 6 MB + 1000 KB | Accepted | Score: 10 | 显示更多 |
Testcase #4 | 5.906 ms | 6 MB + 968 KB | Accepted | Score: 10 | 显示更多 |
Testcase #5 | 5.329 ms | 6 MB + 1016 KB | Accepted | Score: 10 | 显示更多 |
Testcase #6 | 5.008 ms | 7 MB + 36 KB | Accepted | Score: 10 | 显示更多 |
Testcase #7 | 47.548 ms | 6 MB + 260 KB | Accepted | Score: 10 | 显示更多 |
Testcase #8 | 463.796 ms | 29 MB + 568 KB | Accepted | Score: 10 | 显示更多 |
Testcase #9 | 469.043 ms | 27 MB + 776 KB | Accepted | Score: 10 | 显示更多 |
Testcase #10 | 481.775 ms | 31 MB + 888 KB | Accepted | Score: 10 | 显示更多 |