D. Missile Silos
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
A country called Berland consists of n cities, numbered with integer numbers from 1 to n. Some of them are connected by bidirectional roads. Each road has some length. There is a path from each city to any other one by these roads. According to some Super Duper Documents, Berland is protected by the Super Duper Missiles. The exact position of the Super Duper Secret Missile Silos is kept secret but Bob managed to get hold of the information. That information says that all silos are located exactly at a distance l from the capital. The capital is located in the city with number s.
The documents give the formal definition: the Super Duper Secret Missile Silo is located at some place (which is either city or a point on a road) if and only if the shortest distance from this place to the capital along the roads of the country equals exactly l.
Bob wants to know how many missile silos are located in Berland to sell the information then to enemy spies. Help Bob.
Input
The first line contains three integers n, m and s (2 ≤ n ≤ 105, , 1 ≤ s ≤ n) — the number of cities, the number of roads in the country and the number of the capital, correspondingly. Capital is the city no. s.
Then m lines contain the descriptions of roads. Each of them is described by three integers vi, ui, wi (1 ≤ vi, ui ≤ n, vi ≠ ui, 1 ≤ wi ≤ 1000), where vi, ui are numbers of the cities connected by this road and wi is its length. The last input line contains integer l(0 ≤ l ≤ 109) — the distance from the capital to the missile silos. It is guaranteed that:
- between any two cities no more than one road exists;
- each road connects two different cities;
- from each city there is at least one way to any other city by the roads.
Output
Print the single number — the number of Super Duper Secret Missile Silos that are located in Berland.
Examples
input
Copy
4 6 1 1 2 1 1 3 3 2 3 1 2 4 1 3 4 1 1 4 2 2
output
Copy
3
input
Copy
5 6 3 3 1 1 3 2 1 3 4 1 3 5 1 1 2 6 4 5 8 4
output
Copy
3
Note
In the first sample the silos are located in cities 3 and 4 and on road (1, 3) at a distance 2 from city 1 (correspondingly, at a distance 1from city 3).
In the second sample one missile silo is located right in the middle of the road (1, 2). Two more silos are on the road (4, 5) at a distance 3from city 4 in the direction to city 5 and at a distance 3 from city 5 to city 4.
给出一个点s 问边上、顶点上有多 少个点到s点距离为L。
先用最短路跑出s到其它点的最短距离。
判断边的条件。
if ( (dis[u]<nums) && (nums-dis[u]<w) && (w-(nums-dis[u])>nums-dis[v]) )
ans++;
if ( (dis[v]<nums) && (nums-dis[v]<w) && (w-(nums-dis[v])>nums-dis[u]) )
ans++;
if ( (dis[v]<nums) && (dis[u]<nums) && (dis[u]+dis[v]+w==nums*2) )
ans++;
#include<bits/stdc++.h>
#define IL inline
#define RI register int
#define N 200000+200
#define clear(a) memset(a,0,sizeof a)
#define rk for(RI i=1;i<=n;i++)
#define ll long long
using namespace std;
const int maxn=100000+66;
const ll mod=1e9+7;
IL void read(int &x)
{
int f=1;
x=0;
char s=getchar();
while(s>'9'||s<'0')
{
if(s=='-')
f=-1;
s=getchar();
}
while(s<='9'&&s>='0')
{
x=x*10+s-'0';
s=getchar();
}
x*=f;
}
int n,m,root,T,nums;
struct code
{
int u,v,w;
} edge[N],edges[N];
bool vis[N];
int head[N],tot,dis[N],cnt[N];
IL void add(int x,int y,int z)
{
edge[++tot].u=head[x];
edge[tot].v=y;
edge[tot].w=z;
head[x]=tot;
}
IL bool spfa(int now)
{
rk vis[i]=false,dis[i]=2147483647,cnt[i]=false;
queue<int>q;
q.push(now);
vis[now]=true;
dis[now]=0;
while(!q.empty())
{
int u=q.front();
q.pop();
vis[u]=false;
if(cnt[u]>=n)
return true;
for(RI i=head[u]; i; i=edge[i].u)
{
if(dis[edge[i].v]>dis[u]+edge[i].w)
{
dis[edge[i].v]=dis[u]+edge[i].w;
if(!vis[edge[i].v])
{
q.push(edge[i].v);
vis[edge[i].v]=true;
cnt[edge[i].v]++;
if(cnt[edge[i].v]>=n)
return true;
}
}
}
}
return false;
}
int main()
{
read(n),read(m),read(root);
tot=0;
clear(head);
for(RI i=1,u,v,w; i<=m; i++)
{
read(u),read(v),read(w);
add(u,v,w);
add(v,u,w);
edges[i].u=u;
edges[i].v=v;
edges[i].w=w;
}
read(nums);
spfa(root);
int ans=0;
for(int i=1; i<=n; i++)
{
if(dis[i]==nums)
ans++;
}
for (int i=1; i<=m; i++)
{
int u=edges[i].u;
int v=edges[i].v;
int w=edges[i].w;
if ( (dis[u]<nums) && (nums-dis[u]<w) && (w-(nums-dis[u])>nums-dis[v]) )
ans++;
if ( (dis[v]<nums) && (nums-dis[v]<w) && (w-(nums-dis[v])>nums-dis[u]) )
ans++;
if ( (dis[v]<nums) && (dis[u]<nums) && (dis[u]+dis[v]+w==nums*2) )
ans++;
}
printf("%d\n",ans);
}