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 nm and s (2 ≤ n ≤ 105, 

Codeforces 144D. Missile Silos 最短路  堆优化的Dijkstra+推导_i++

, 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 viuiwi (1 ≤ vi, ui ≤ nvi ≠ ui, 1 ≤ wi ≤ 1000), where viui 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 11 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 33 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.

 

题意:

最短距离为len的地方有几个(在边上或者在点上都算)?。

分析:

对于节点上:

直接Dijkstra最短路求出dist[i]来,if(dist[i]==len) ans++

对于边上的点上:

我们枚举每一条边,分为三种情况:

第一种情况点p靠经u:注意这里的靠经是指:dist[u]+|up|<dist[v]+|vp|,不要被表面迷惑

Codeforces 144D. Missile Silos 最短路  堆优化的Dijkstra+推导_i++_02

满足上述情况:u距离s的距离小于len,|uv|这条边的距离+dist[u]>len,s经过v经过p的最短距离要大于len

第二种情况点p靠经v,与上述同理

第三种情况p在uv中间,即s经过u到p距离,s经过v到p的距离均为len。(注意p不一定是uv的中点,而是svp=sup,所以在这种情况下p的位置不确定)

#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int N=200005;
int n,m,st,en;
bool vis[N];//标记数组
ll dist[N];//距离数组
int pre[N];//前驱
int head[N],tot;
//结构体数组edge存边,edge[i]表示第i条边,
//head[i]存以i为起点的第一条边(在edge中的下标)
struct node
{
int next; //下一条边的存储下标
int from,to; //这条边的终点
ll w;//权值
} edge[N*4];
///tot为边的计数,从0开始计,每次新加的边作为第一条边,最后倒序遍历
void add(int u,int v,ll w)
{
edge[tot].from=u;
edge[tot].to=v;
edge[tot].w=w;
edge[tot].next=head[u];
head[u]=tot++;
}
void init()
{
tot=0;
memset(head,-1,sizeof(head));
memset(pre,-1,sizeof(pre));
}

struct Rec
{
int id; //节点编号
ll dis; //距离
/*bool operator<(const Rec &tmp)const{
return dis>tmp.dis;
} */
};
bool operator <(const Rec&a,const Rec&b)
{
return a.dis>b.dis;
}
priority_queue<Rec> q;//重载小于号实现小根堆

void dijkstra()
{
for(int i=0; i<=n; i++)
dist[i]=1e18;
memset(vis,0,sizeof(vis));

dist[st]=0;
Rec rec;
rec.id=st;
rec.dis=0;
q.push(rec);
while(!q.empty())
{
int u=q.top().id;

q.pop();
if(vis[u])
continue;
vis[u]=1;
for(int i=head[u]; i!=-1; i=edge[i].next) //遍历以u为起点的所有边,与输入顺序相反
{
int v=edge[i].to;
ll w=edge[i].w;
if(dist[v]>dist[u]+w)
{
dist[v]=dist[u]+w;
Rec rec;
rec.id=v;
rec.dis=dist[v];
q.push(rec);
pre[v]=u;
}
}
}
}
vector<int> path;
void getPath()
{
for(int i=en; i!=-1; i=pre[i])
{
path.push_back(i);
}
}
int main()
{

int s;
scanf("%d%d%d",&n,&m,&s);
st=s;
en=n;
int u,v;
ll w;
init();
for(int i=1; i<=m; i++)
{
scanf("%d%d%lld",&u,&v,&w);
add(u,v,w);
add(v,u,w);
}
ll len;
scanf("%lld",&len);
dijkstra();

ll ans=0;
for(int i=1; i<=n; i++)
{
if(dist[i]==len)
ans++;
}

for(int i=0; i<tot; i+=2)
{

u=edge[i].from;
v=edge[i].to;
w=edge[i].w;
//cout<<u<<"*"<<dist[u]<<" "<<v<<"*"<<dist[v]<<" "<<w<<endl;
if(dist[u]<len&&(len-dist[u])<w&&w-(len-dist[u])+dist[v]>len)
ans++;
if(dist[v]<len&&(len-dist[v])<w&&w-(len-dist[v])+dist[u]>len)
ans++;
if(dist[u]<len&&dist[v]<len&&dist[u]+dist[v]+w==2*len)

ans++;
//cout<<ans<<endl;

}
cout<<ans<<endl;

return 0;

}