codeforces 144D Missile Silos(最短路)

通过一次最短路径算法遍历,确定图中所有距离指定城市s为d的城市及道路位置。适用于竞赛编程中的图论问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转载请注明出处: http://www.cnblogs.com/fraud/           ——by fraud

 

Missile Silos

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.

Sample test(s)
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.

题意:

给出一张图,问图上所有到s点的距离为d的有几个点。

 

一遍最短路,得到s到所有点的最短距离。然后在枚举每条边,统计边上是否有满足要求的点。

  1 #include <iostream>
  2 #include <sstream>
  3 #include <ios>
  4 #include <iomanip>
  5 #include <functional>
  6 #include <algorithm>
  7 #include <vector>
  8 #include <string>
  9 #include <list>
 10 #include <queue>
 11 #include <deque>
 12 #include <stack>
 13 #include <set>
 14 #include <map>
 15 #include <cstdio>
 16 #include <cstdlib>
 17 #include <cmath>
 18 #include <cstring>
 19 #include <climits>
 20 #include <cctype>
 21 using namespace std;
 22 #define XINF INT_MAX
 23 #define INF 0x3FFFFFFF
 24 #define MP(X,Y) make_pair(X,Y)
 25 #define PB(X) push_back(X)
 26 #define REP(X,N) for(int X=0;X<N;X++)
 27 #define REP2(X,L,R) for(int X=L;X<=R;X++)
 28 #define DEP(X,R,L) for(int X=R;X>=L;X--)
 29 #define CLR(A,X) memset(A,X,sizeof(A))
 30 #define IT iterator
 31 typedef long long ll;
 32 typedef pair<int,int> PII;
 33 typedef vector<PII> VII;
 34 typedef vector<int> VI;
 35 #define MAXN 100100
 36 vector<PII> Map[MAXN];
 37 
 38 //清空邻接表 
 39 void init() { REP(i,MAXN) Map[i].clear(); }
 40 
 41 //求以s为源点的最短路 结果保存在dis中 
 42 int dis[MAXN];
 43 void dijkstra(int s)
 44 {
 45     REP(i,MAXN){dis[i]=i==s?0:INF;}
 46     int vis[MAXN] = {0};
 47     priority_queue<PII, vector<PII>, greater<PII> > q;
 48     q.push(MP(0,s));
 49     while(!q.empty())
 50     {
 51         PII p = q.top(); q.pop();
 52         int x = p.second;
 53         if(vis[x])continue;
 54         vis[x] = 1;
 55         for(vector<PII>::iterator it = Map[x].begin(); it != Map[x].end(); it++)
 56         {
 57             int y = it->first;
 58             int d = it->second;
 59             if(!vis[y] && dis[y] > dis[x] + d)
 60             {
 61                 dis[y] = dis[x] + d;
 62                 q.push(MP(dis[y],y));
 63             }
 64         }
 65     }
 66 }
 67 
 68 struct node
 69 {
 70     int u,v,d;
 71 }edge[MAXN];
 72 int main()
 73 {
 74     ios::sync_with_stdio(false);
 75     int n,m,s;
 76     while(cin>>n>>m>>s)
 77     {
 78         int u,v,d;
 79         init();
 80         for(int i=0;i<m;i++)
 81         {
 82             cin>>u>>v>>d;
 83             u--;
 84             v--;
 85             Map[u].PB(MP(v,d));
 86             Map[v].PB(MP(u,d));
 87             edge[i].u=u;
 88             edge[i].v=v;
 89             edge[i].d=d;
 90         }
 91         int l;
 92         cin>>l;
 93         s--;
 94         dijkstra(s);
 95         int ans=0;
 96         for(int i=0;i<n;i++)
 97         {
 98             if(dis[i]==l)ans++;
 99         }
100         for(int i=0;i<m;i++)
101         {
102             u=edge[i].u;
103             v=edge[i].v;
104             d=edge[i].d;
105             if(dis[u]>dis[v])swap(u,v);
106             if(dis[v]-dis[u]==d)
107             {
108                 if(l>dis[u]&&l<dis[v])ans++;
109             }
110             else
111             {
112                 int x=l-dis[u];
113                 if(x<=0)continue;
114                 if(x>d)continue;
115                 if(dis[v]>l&&x<d)
116                 {
117                     ans++;
118                     continue;
119                 }
120                 if(dis[v]==l&&x<d)
121                 {
122                     ans++;
123                     continue;
124                 }
125                 int y=l-dis[v];
126                 if(x+y==d)
127                 {
128                     ans++;
129                     continue;
130                 }
131                 if(x<d-y)ans++;
132                 if(y<d-x)ans++;
133             }
134         }
135         cout<<ans<<endl;
136     }
137         
138             
139     return 0;
140 }
代码君

 

转载于:https://www.cnblogs.com/fraud/p/4338521.html

### 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); } } } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值