## 大吉大利，晚上吃鸡！

``````#include<bits/stdc++.h>
using namespace std;
#define int long long
{
int f=1,w=0;char x=0;
while(x<'0'||x>'9') {if(x=='-') f=-1; x=getchar();}
while(x!=EOF&&x>='0'&&x<='9') {w=(w<<3)+(w<<1)+(x^48);x=getchar();}
return w*f;
}
const int N=5e5+10;
map<int,int> MP;
vector<int> LL[N],RR[N];
int num_edge,n,m,S,T,Sum,ans;
int Dis[N][2],Vis[N],Dag[N],R[N],f[N];
struct Edge{int next,to,dis;} edge[N<<1];
inline void Add(int from,int to,int dis)
{
edge[num_edge].dis=dis;
edge[num_edge].to=to;
}
inline void Dijstra_For_Count(int X,int Typ)
{
priority_queue<pair<int,int> > Q;
Q.push(make_pair(0,X));
memset(Vis,0,sizeof(Vis));
memset(Nex,0,sizeof(Nex));
Cnt[X][Typ]=1,Dis[X][Typ]=0;
while(Q.size())
{
int x=Q.top().second;Q.pop();
if(Vis[x]) continue;Vis[x]=1;
if(Dis[edge[i].to][Typ]>Dis[x][Typ]+edge[i].dis)
{
Dis[edge[i].to][Typ]=Dis[x][Typ]+edge[i].dis;
Cnt[edge[i].to][Typ]=Cnt[x][Typ],Nex[edge[i].to]=x;
Q.push(make_pair(Dis[edge[i].to][Typ],edge[i].to));
}
else if(Dis[edge[i].to][Typ]==Dis[x][Typ]+edge[i].dis)
Cnt[edge[i].to][Typ]+=Cnt[x][Typ];
}
}
inline void TopSort(int Typ)
{
memset(Dag,0,sizeof(Dag));
queue<int> Q;
for(int i=1;i<=n;i++)
if(Dis[edge[j].to][Typ^1]+Dis[i][Typ]+edge[j].dis==Dis[0][T])
Dag[edge[j].to]++;
for(int i=1;i<=n;i++) if(!Dag[i]) Q.push(i);
while(Q.size())
{
int x=Q.front();Q.pop();
if(Dis[edge[i].to][Typ^1]+Dis[x][Typ]+edge[i].dis==Dis[0][T])
{
if(!(--Dag[edge[i].to])) Q.push(edge[i].to);
if(!Typ) L[edge[i].to]=max(L[edge[i].to],L[x]);
else R[edge[i].to]=min(R[edge[i].to],L[x]);
}
}
}
signed main(){
#ifndef ONLINE_JUDGE
freopen("A.in","r",stdin);
#endif
memset(Dis,0x3f,sizeof(Dis));
for(int i=1,u,v,d;i<=m;i++)
Dijstra_For_Count(S,0);Dijstra_For_Count(T,1);
if(!Cnt[T][0]) {printf("%lld\n",n*(n-1)/2);return 0;}
for(int i=S;i;i=Nex[i]) Rod[++Sum]=i,L[i]=Sum+1,R[i]=Sum-1;
for(int i=1;i<=n;i++) if(!L[i]&&!R[i]) L[i]=1,R[i]=Sum;
TopSort(0);TopSort(1);
for(int i=1;i<=n;i++)
{
if(Dis[i][0]+Dis[i][1]==Dis[T][0]) f[i]=Cnt[i][0]*Cnt[i][1];
if(L[i]>R[i]) continue;LL[L[i]].push_back(i);RR[R[i]].push_back(i);
}
for(int i=1;i<=Sum;i++)
{
for(int j=0;j<(int)LL[i].size();j++) ++MP[f[LL[i][j]]];
ans+=MP[f[T]-f[Rod[i]]];
for(int j=0;j<(int)RR[i].size();j++) --MP[f[RR[i][j]]];
}
printf("%lld",ans);
}   ``````