题干:

A country called Berland consists of n cities, numbered with integer numbers from 1to 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, , 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

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的地方有几个(在边上或者在点上都算)。

解题报告:

这题思路还是很清晰的啊,但是1WA了因为开数组应该是2e5,因为是无向图啊!!我记得之前就在这错过。

首先把在点上的处理了,然后枚举每一条边分四种情况分别列出来,其中最后一种再分三种情况列出来就好了:

1.先可以设定的点在u,v中间,

2.可以设定的点更加靠近u,这样只需要满足求出的那个点,经过v再到原点的距离>L 就可以ans++了;

3.可以设定的点更加靠近v,这样只需要满足求出的那个点,经过u到原点的距离>L就可以ans++了。

对应将三种情况分别枚举出来,维护ans即可。(2和3比较难绕,但是可以从答案贡献的角度反面考虑,就不难证明正确性。)

AC代码:

#include<cstdio>
#include<queue>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAX = 2e5 +5;
const int INF = 0x3f3f3f3f;
int n,m,s;
int id,ans;
int head[MAX];
int dis[MAX];
bool vis[MAX];
struct Node {int fm,to,w;int ne;
} e[MAX];
struct Point {int pos;int c;Point(){}Point(int pos,int c):pos(pos),c(c){}bool operator <(const Point & b) const {return c > b.c;}
};
void add(int u,int v,int w) {e[++id].fm=u;e[id].to = v;e[id].w = w;e[id].ne = head[u];head[u] = id;
}
void Dijkstra(int u) {memset(dis,INF,sizeof dis);dis[u] = 0;memset(vis,0,sizeof vis);priority_queue<Point> pq;pq.push(Point(u,0));while(!pq.empty()) {Point cur = pq.top();pq.pop();if(vis[cur.pos] == 1) continue;vis[cur.pos] = 1;for(int i = head[cur.pos]; i!=-1; i=e[i].ne) {if(vis[e[i].to]) continue;if(dis[e[i].to] > dis[cur.pos] + e[i].w) {dis[e[i].to] = dis[cur.pos] + e[i].w;pq.push(Point(e[i].to,dis[e[i].to]));}}}
}
int main()
{cin>>n>>m>>s;memset(head,-1,sizeof head);ans = 0;int a,b,w,L;for(int i = 1; i<=m; i++) {scanf("%d%d%d",&a,&b,&w);add(a,b,w);add(b,a,w);}cin>>L;Dijkstra(s);for(int i = 1; i<=n; i++) {if(dis[i] == L) ans++;}//枚举每一条边 for(int i = 1; i<=id; i+=2) {int u = e[i].fm,v = e[i].to,w = e[i].w;if(dis[u] >= L && dis[v] >= L) continue;if(dis[u] >= L && dis[v] <  L) {if(dis[v] + w > L) ans++; }else if(dis[v] >=L && dis[u] < L) {if(dis[u] + w > L) ans++;}else {int disu = L - dis[u],disv = L - dis[v];//计算一下多出来的那一块if(disu + disv == w) ans++;else {if(disu < w && dis[v] + (w-disu) > L) ans++;//别忘了disu < wif(disv < w && dis[u] + (w-disv) > L) ans++; }}}printf("%d\n",ans);return 0 ;
}

【CodeForces - 144D】Missile Silos(单源最短路,枚举中间边,枚举情况可能性)相关推荐

  1. codeforces 144D Missile Silos(最短路)

    转载请注明出处: http://www.cnblogs.com/fraud/           --by fraud Missile Silos A country called Berland c ...

  2. Codeforces 144D. Missile Silos 最短路

    D. Missile Silos time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  3. Codeforces 144D: Missile Silos

    链接:http://codeforces.com/problemset/problem/144/D 题意:题目给你一个无向边权图,然后给你一个点s,和长度l,问距离点s长度为l的点有多少,这些点可以在 ...

  4. codeforces 144D Missile Silos spfa

    题意:给定一张地图,可以在边和点上设立发射井,其中发射井到首都的距离必须等于l 做法:十万个点,竟然可以求最短路,而且最后时间还只有区区125ms.可能是边太少的原因... 图中的边可以分成两类,一种 ...

  5. acwing单源最短路的建图模式总结

    .根据边权的范围以及问题求解的需要,最短路问题可以分为以下 4 种情形,分别用不同的算法求解. • 单源最短路径(固定一个顶点为原点,求源点到其他每个顶点 的最短路径) • 1. 边权非负:Dijks ...

  6. 算法提高课-图论-差分约束- AcWing 1169. 糖果:spfa求单源最短路、差分约束

    文章目录 题目分析 题目链接 题目分析 来源:acwing 分析: 差分约束系统 差分约束系统是一种特殊的N元一次不等式组.它包含N个变量X1,...,XnX_1,...,X_nX1​,...,Xn​ ...

  7. CSP认证201609-4 交通规划[C++题解]:最短路径树、dijkstra求单源最短路、递推思想

    题目分析 来源:acwing 分析: 这题是最短路树.保持原图中所有点到根结点的最短距离不变,然后在原图中选择一些边,使所有点连通的最短路是多长. 最短路径树,是一种使用最短路径算法生成的数据结构树. ...

  8. 算法提高课-图论-单源最短路的综合应用-AcWing 1135. 新年好:dijkstra和dfs暴搜结合

    题目分析 来源:acwing 分析: 先预处理出从1,a,b,c,d,e出发到其他所有点的单源最短路.存在二维数组dist[6][N]中 dfs暴搜所有拜访顺序,共有5!种,对于每一种拜访顺序,可以通 ...

  9. 算法提高课-图论-单源最短路的建图方式-AcWing 920. 最优乘车:bfs求最短路、建图

    题目分析 来源:acwing 分析: 本题难在抽象建图上,这里采用的建图方式是:同一条公交线路上,前面的站点都可以连一条有向边到其后面的站点,且边权都为1. 由于边权都是1,可以用bfs来求最短路. ...

  10. PAT甲级1111 Online Map (30分):[C++题解]两次dijkstra求单源最短路、保存路径、长度最短、时间最短

    文章目录 题目分析 题目链接 题目分析 来源:acwing 分析:dijkstra求单源最短路的题目. 只是写两遍而已,第一遍求按照路径长度求,第二遍按照时间最少求. 另外加一个vector路径的判断 ...

最新文章

  1. 网络地址和广播地址的作用
  2. Isomorphic Strings
  3. html批量翻译 github,英文单词短语批量翻译工具WordListTranslator
  4. Mybatis Plus——以XML方式使用 Wrapper 自定义SQL时IDEA错误[**expected, got ‘${‘]解决方案
  5. C#的变迁史10 - C# 5.0 之其他增强篇
  6. java递归实现多级菜单栏_Java构建树形菜单以及支持多级菜单的实例代码
  7. 虚拟机VirtualBox中Ubuntu无法全屏解决方法
  8. 深度学习中Attention机制的“前世今生”
  9. linux原子方式,linux – 以原子方式移动目录
  10. 使用VS自带的打包工具,制作winform安装项目
  11. 6-1 二叉搜索树的操作集 (30 分)
  12. JS判断手机浏览器(转)
  13. jQuery源码解读一
  14. 用HTML5为你的网页添加音效(兼容Firefox 3.5+, IE 6-9, Safari 3.0+, Chrome 3.0+, Opera 10.5+)...
  15. Word 如何删除分节符?
  16. linux装在机械硬盘怎么样,电脑装了固态硬盘还能再装机械硬盘吗
  17. 牧羊人和金斧子|不要和产品聊天!
  18. 批量下载人像图片的技巧,POCO相册图片如何下载的方法
  19. 只有两个键的键盘,只会复制粘贴
  20. 局域网唤醒计算机,电脑远程开机_局域网唤醒电脑 | 茶杯猫

热门文章

  1. 2018蓝桥模拟赛·天上的星星 暴力|二维树状数组
  2. php100教程源码,PHP100 视频教程 2012-2013版_PHP教程
  3. threejs指定对象旋转中心
  4. apt-get的更新源
  5. python traceback 丢失_基于python traceback实现异常的获取与处理
  6. 测试鼠标双击_鼠标环境可靠性测试是什么
  7. uniapp开发实例github_跨端开发痛点?送你一款Vue最流行的跨端框架——uni-app
  8. 由于找不到openni2_Kinect开发教程八:OpenNI2显示深度、彩色及融合图像
  9. 电脑用linux命令大全,电脑操作时常用的一些Linux命令
  10. 【转】Dynamics CRM:“the given key was not present in the dictionary”