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

SPFA算法求导弹基地数量
本文介绍了一种使用SPFA算法解决特定图论问题的方法,即在一个带有多个节点和边的图中寻找距离指定起点最短路径等于给定长度l的所有位置数量。通过两遍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();
    }
}






### Codeforces 平台上的短路径算法题目 #### Dijkstra 算法的应用实例 当面对构建短路径树的需求时,可以采用Dijkstra算法来解决。该方法的核心在于优先选择当前节点能够延伸出去的多条候选边中具有小权重的一条作为扩展方向;而在仅存在唯一一条可能的拓展边的情况下,则直接选取这条边继续探索过程[^1]。 ```python import heapq def dijkstra(graph, start): n = len(graph) dist = [float('inf')] * n dist[start] = 0 heap = [(0, start)] while heap: current_dist, u = heapq.heappop(heap) if current_dist != dist[u]: continue for v, weight in graph[u].items(): alt = dist[u] + weight if alt < dist[v]: dist[v] = alt heapq.heappush(heap, (alt, v)) return dist ``` #### Floyd-Warshall 算法处理复杂情况下的优化策略 对于涉及多个源点之间的短路径计算问题,Floyd-Warshall是一个有效的解决方案。然而,在某些特定场景下(比如本题),通过预先给定的信息可以直接定位受影响的部分并加以简化,从而避免不必要的全量遍历操作,达到降低时间复杂度的效果。值得注意的是,在累加过程中应当选用`long long`类型的变量以防止溢出错误的发生[^2]。 #### 单源短路径查询案例解析 考虑到从指定起点出发前往其余各个顶点间的短距离需求,此情形适用于单源短路径类别的算法实现方式。具体而言,即是从某固定位置开始测量至其它任意可达地点的距离长度,并针对不可达的情形输出特殊标记值 `-1` 表明无法访问的状态[^3]。 ```cpp #include<bits/stdc++.h> using namespace std; const int INF=0x3f3f3f; vector<pair<int,int>> adj[100]; int dis[100]; void spfa(int src){ queue<int> q; vector<bool> vis(100,false); fill(dis,dis+100,INF); dis[src]=0;q.push(src); while(!q.empty()){ int cur=q.front();q.pop(); vis[cur]=false; for(auto& edge : adj[cur]){ int next=edge.first,cost=edge.second; if(dis[next]>dis[cur]+cost){ dis[next]=dis[cur]+cost; if(!vis[next]){ vis[next]=true; q.push(next); } } } } } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值