Codeforces 144 D Missile Silos【最短路SPFA+枚举】

来源:互联网 发布:天下霸图2 优化 编辑:程序博客网 时间:2024/06/04 20:12

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 from1 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 distancel from the capital. The capital is located in the city with numbers.

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 exactlyl.

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 integersvi,ui,wi (1 ≤ vi, ui ≤ n,vi ≠ ui,1 ≤ wi ≤ 1000), wherevi,ui are numbers of the cities connected by this road andwi is its length. The last input line contains integerl (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
4 6 11 2 11 3 32 3 12 4 13 4 11 4 22
Output
3
Input
5 6 33 1 13 2 13 4 13 5 11 2 64 5 84
Output
3
Note

In the first sample the silos are located in cities 3 and4 and on road (1, 3) at a distance2 from city 1 (correspondingly, at a distance1 from 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 3 from city 4 in the direction to city 5 and at a distance3 from city 5 to city4.


题目大意:

图中有n个点,m条无向边,一个原点,问有距离原点最短距离为l的地方有几个(边上点上都算)。


思路:


1、首先对建好的图跑一遍SPFA。


2、然后枚举所有边(注意我们枚举的是边,不包含两个),对应会出现三种情况:

①可以设定的点更加靠近u,dis【u】<l&&dis【u】+w>l&&w-(l-dis【u】)>l-dis【v】;

②可以设定的点更加靠近v,dis【v】<l&&dis【v】+w>l&&w-(l-dis【v】)>l-dis【u】;

③可以设定的点在u,v中间:dis【u】<l&&dis【v】<l&&dis【u】+dis【v】+w==l*2;

对应将三种情况分别枚举出来,维护ans即可。


Ac代码:


#include<stdio.h>#include<string.h>#include<queue>using namespace std;struct node{    int from;    int to;    int next;    int num;    int w;}e[1515151];int dis[100500];int vis[100500];int head[100500];int n,m,ss,l,cont;void add(int from,int to,int w){    e[cont].from=from;    e[cont].to=to;    e[cont].num=cont;    e[cont].w=w;    e[cont].next=head[from];    head[from]=cont++;}void SPFA(){    memset(vis,0,sizeof(vis));    for(int i=1;i<=n;i++)dis[i]=0x3f3f3f3f;    queue<int >s;    s.push(ss);    dis[ss]=0;    while(!s.empty())    {        int u=s.front();        vis[u]=0;        s.pop();        for(int i=head[u];i!=-1;i=e[i].next)        {            int v=e[i].to;            int w=e[i].w;            if(dis[v]>dis[u]+w)            {                dis[v]=dis[u]+w;                if(vis[v]==0)                {                    vis[v]=1;                    s.push(v);                }            }        }    }}void Slove(){    SPFA();    int output=0;    for(int i=1;i<=n;i++)    {        if(dis[i]==l)output++;    }    for(int i=0;i<cont;i++)    {        if(i%2==1)continue;        int u=e[i].from;        int v=e[i].to;        int w=e[i].w;        if(dis[u]<l&&dis[u]+w>l&&w-(l-dis[u])>l-dis[v])output++;        if(dis[v]<l&&dis[v]+w>l&&w-(l-dis[v])>l-dis[u])output++;        if(dis[u]<l&&dis[v]<l&&dis[u]+dis[v]+w==l*2)output++;    }    printf("%d\n",output);}int main(){    while(~scanf("%d%d%d",&n,&m,&ss))    {        cont=0;        memset(head,-1,sizeof(head));        for(int i=0;i<m;i++)        {            int x,y,w;            scanf("%d%d%d",&x,&y,&w);            add(x,y,w);            add(y,x,w);        }        scanf("%d",&l);        Slove();    }}






0 0