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

题干:

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 ;
}

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441477.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

第九大陆服务器未找到文件,第九大陆服务器优化规则说明 拍卖行和寄售功能关闭公告...

第九大陆已经确定了六月份服务器的优化时间与规则&#xff0c;同时为了配合6月8日的服务器优化实施&#xff0c;拍卖行将于6月5日关闭&#xff0c;待服务器优化完成后即可开放&#xff01;给大家带来的不便&#xff0c;敬请您谅解&#xff01;《第九大陆》服务器优化详细规则说…

【牛客 - 210A】游戏(思维,脑洞)

题干&#xff1a; BLUESKY007,fengxunling和dreagonm三个人发现了一个像素游戏,这款神奇的游戏每次会生成一个nxm的网格,其中每一个格子都被随机染色为R,G,B三种颜色之一,每次都可以选择任意一个非B颜色的格子进行一次操作,每次操作都会满足以下规则&#xff1a; 1.操作的范围为…

建房子 最安全图纸_妄想山海初期该怎么办?砍树狩猎建房子,还能拆别人的房子...

妄想山海还是一个很有趣的游戏&#xff0c;唯一的缺点就是游戏对手机配置要求比较高&#xff0c;手机太差的玩家&#xff0c;就别想那么多了&#xff0c;希望等游戏正式上线&#xff0c;能优化的好一些吧&#xff01;在测试中&#xff0c;玩家能体验到游戏中的各种玩法&#xf…

【POJ - 2226】Muddy Fields(匈牙利算法 或 网络流dinic,二分图匹配,最小点覆盖,矩阵中优秀的建图方式 )

题干&#xff1a; Rain has pummeled the cows field, a rectangular grid of R rows and C columns (1 < R < 50, 1 < C < 50). While good for the grass, the rain makes some patches of bare earth quite muddy. The cows, being meticulous grazers, dont w…

springboot的原生cache_springboot-shiro-redis-session-cache

1.配置mysql和redis redis的需要配置密码 原生jedis的使用相对于spring-data-redis更加灵活 所以本项目用原生jedis来直接作为缓存使用 具体使用方法请参看相关配置 特别注意如果mysql是5.7以上版本&#xff0c;请去掉sql_mode中的ONLY_FULL_GROUP_BY 本项目用户支持多角色多部…

【51Nod - 1272 】最大距离 (思维,排序sort的空间优化)

题干&#xff1a; 给出一个长度为N的整数数组A&#xff0c;对于每一个数组元素&#xff0c;如果他后面存在大于等于该元素的数&#xff0c;则这两个数可以组成一对。每个元素和自己也可以组成一对。例如&#xff1a;{5, 3, 6, 3, 4, 2}&#xff0c;可以组成11对&#xff0c;如…

javaweb在线问卷系统_2020 最新流行的Java Web报表工具比对

随着信息系统的高速发展&#xff0c;报表平台逐渐成为了信息系统当中最为核心和重要的功能模块。报表工具有助于将原始数据可视化显示&#xff0c;使决策者或者相关人员能够一览整体的数据趋势&#xff0c;完整的报表解决方案会提供多样的表格数据展示、数据可视化元素&#xf…

python 深copy_python中的深copy和浅copy

bytesPython bytes/strbytes 在Python3中作为一种单独的数据类型&#xff0c;不能拼接&#xff0c;不能拼接&#xff0c;不能拼接>>> €20.encode(utf-8)b\xe2\x82\xac20>>> b\xe2\x82\xac20.decode(utf-8)€20解码>>> b\xa420.decode(windows-1255…

【HDU - 4185】Oil Skimming (二分图,建图,匈牙利算法)

题干&#xff1a; Thanks to a certain "green" resources company, there is a new profitable industry of oil skimming. There are large slicks of crude oil floating in the Gulf of Mexico just waiting to be scooped up by enterprising oil barons. One …

python pandas read_csv 迭代器使用方法_pandas.read_csv参数详解(小结)

更多python教程请到友情连接&#xff1a; 菜鸟教程www.piaodoo.com人人影视www.sfkyty.com飞卢小说网www.591319.com韩剧网www.op-kg.com兴化论坛www.yimoge.cn星辰影院www.hhsos.netpandas.read_csv参数整理读取CSV(逗号分割)文件到DataFrame也支持文件的部分导入和选择迭代参…

【HDU - 2398 】Savings Account (水题模拟)

题干&#xff1a; Suppose you open a savings account with a certain initial balance. You will not make any withdrawals or further deposits for a number of years. The bank will compound your balance (add the annual interest) once a year, on the anniversary …

隧道凿岩机器人_隧道凿岩机器人的研制

隧道凿岩机器人的研制隧道、洞室开挖是现代交通、能源、采掘、建筑等大规模基本建设中的一项难度大、耗资耗时多、劳动条件差但又十分重要、十分关键的施工作业。早期的液压凿岩设备全都是由人工操作的液压凿岩钻车&#xff0c;其施工效率和施工精度完全取决于操作人员的熟练程…

【HDU - 1045】Fire Net (dfs 或二分图)

题干&#xff1a; Suppose that we have a square city with straight streets. A map of a city is a square board with n rows and n columns, each representing a street or a piece of wall. A blockhouse is a small castle that has four openings through which to …

html 按钮 按下 状态_第一次按下是启动,第二次按下是停止,俵哥分享2种接线方法...

朋友们大家好我是大俵哥&#xff0c;今天我们来说一下单按钮启停电路。这个电路虽然应用的不多&#xff0c;但是非常的经典&#xff0c;新手朋友们可以拿来练手。今天我们讲2种控制方法&#xff0c;一种用中间继电器控制一种用时间继电器控制&#xff0c;在看电路之前&#xff…

【 CodeForces - 1060B 】Maximum Sum of Digits(思维,构造)

题干&#xff1a; You are given a positive integer nn. Let S(x)S(x) be sum of digits in base 10 representation of xx, for example, S(123)1236S(123)1236, S(0)0S(0)0. Your task is to find two integers a,ba,b, such that 0≤a,b≤n0≤a,b≤n, abnabn and S(a)S(…

mysql 异步_MySQL -- 异步I/O

linux上&#xff0c;innodb使用异步IO子系统(native AIO)来对数据文件页进行预读和写请求。行为受到参数innodb_use_native_aio控制。默认是开启的&#xff0c;且只是适用于linux平台&#xff0c;需要libaio库。在其他的类unix平台上&#xff0c;innodb使用的是同步I/O。由于历…

【CodeForces - 633D】Fibonacci-ish (离散化,暴力枚举+STPmap,fib数列收敛性质)

题干&#xff1a; Yash has recently learnt about the Fibonacci sequence and is very excited about it. He calls a sequence Fibonacci-ish if the sequence consists of at least two elementsf0 and f1 are arbitraryfn  2  fn  1  fn for all n ≥ 0. You …

sql server 迁移 mysql_【转】sql server迁移到mysql

【1】MSSQL2SQLSQL Server转换为MySQL工具&#xff0c;用了一下 感觉蛮不错的。分享上来&#xff0c;同时也以便记录下来以后自用。工具名称&#xff1a;Mss2sql来个操作流程:下载后打开压缩包运行mss2sql默认就是Move to MysQL server directly,选择下一步继续下一步,稍等片刻…

【51Nod - 1268】和为K的组合 (背包 或 dfs)

题干&#xff1a; 给出N个正整数组成的数组A&#xff0c;求能否从中选出若干个&#xff0c;使他们的和为K。如果可以&#xff0c;输出&#xff1a;"Yes"&#xff0c;否则输出"No"。 Input 第1行&#xff1a;2个数N, K, N为数组的长度, K为需要判断的和(…

centos7 mysql tar_CentOS7中mysql-5.7.21-el7-x86_64.tar.gz版MySQL的安装与配置

一、准备阶段通常情况下&#xff0c;MySQL在CentOS下主要使用glibc、rpm、yum等方式进行安装&#xff0c;使用mysql-5.7.21-el7-x86_64.tar.gz包进行安装的很少见&#xff0c;网上资料也较少。通过一上午的摸索&#xff0c;总结出如下安装方法。下载安装包&#xff1a;[rootGee…