#include<cstdio> #include<algorithm> #include<cstring> using namespace std; const int maxn=100000+10; const int nil=1000000000; struct my{ int next; int v; int w; }; bool vis[maxn][3]; int cnt[maxn][3],dist[maxn][3],fa,adj[maxn]; int n,m; my bian[maxn*2]; void myinsert(int u,int v,int w){ bian[++fa].v=v; bian[fa].next=adj[u]; bian[fa].w=w; adj[u]=fa; } void dijikstar(int s,int t){ for (int i=1;i<=n;i++) dist[i][1]=dist[i][2]=nil; cnt[s][1]=1; dist[s][1]=0; int falg,u; for (int i=1;i<2*n;i++){ int minn=nil; for (int j=1;j<=n;j++){ if(!vis[j][1]&&dist[j][1]<minn){ minn=dist[j][1]; u=j; falg=1; } else if(!vis[j][2]&&dist[j][2]<minn){ minn=dist[j][2]; u=j; falg=2; } } if(minn==nil) break; vis[u][falg]=1; for (int j=adj[u];j;j=bian[j].next){ int v=bian[j].v; int w=bian[j].w; if(dist[v][1]>minn+w){ dist[v][2]=dist[v][1]; cnt[v][2]=cnt[v][1]; dist[v][1]=minn+w; cnt[v][1]=cnt[u][falg]; } else if(dist[v][1]==minn+w) cnt[v][1]+=cnt[u][falg]; else if(dist[v][2]>minn+w){ dist[v][2]=minn+w; cnt[v][2]=cnt[u][falg]; } else if(dist[v][2]==minn+w) cnt[v][2]+=cnt[u][falg]; } } int ans=0; if(dist[t][2]==dist[t][1]+1) ans=cnt[t][2]+cnt[t][1]; else ans=cnt[t][1]; printf("%d ",ans); } int main(){ int u,v,w; int t; scanf("%d",&t); while(t--){ scanf("%d%d",&n,&m); int s,t; memset(bian,0,sizeof(bian)); memset(adj,0,sizeof(adj)); memset(cnt,0,sizeof(cnt)); memset(vis,0,sizeof(vis)); for (int i=1;i<=m;i++){ scanf("%d%d%d",&u,&v,&w); myinsert(u,v,w); } scanf("%d%d",&s,&t); dijikstar(s,t); } return 0; }