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

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
4 6 1
1 2 1
1 3 3
2 3 1
2 4 1
3 4 1
1 4 2
2
Output
3
Input
5 6 3
3 1 1
3 2 1
3 4 1
3 5 1
1 2 6
4 5 8
4
Output
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 1 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 distance 3 from city 5 to city 4.


题目大意:

图中有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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值