Codeforces 144D Missile Silos 最短路
2 seconds
256 megabytes
standard input
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.
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.
Print the single number — the number of Super Duper Secret Missile Silos that are located in Berland.
4 6 1
1 2 1
1 3 3
2 3 1
2 4 1
3 4 1
1 4 2
2
3
5 6 3
3 1 1
3 2 1
3 4 1
3 5 1
1 2 6
4 5 8
4
3
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 1from 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 distance3 from city 4 in the direction to city 5 and at a distance 3 from city 5 to city 4.
#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
typedef long long ll;
const int N = 1e5 + ; struct qnode {
int v;
int c;
qnode(int _v = , int _c = ) : v(_v), c(_c) { }
bool operator < (const qnode &r) const {
return c > r.c;
};
};
struct Edge {
int v, cost;
Edge(int _v = , int _cost = ) : v(_v), cost(_cost) { }
};
vector<Edge> E[N];
bool vis[N];
int dist[N], pre[N]; void Dij(int n, int s) {
memset(vis, false, sizeof vis);
for(int i = ; i <= n; ++i) dist[i] = INF;
priority_queue<qnode> que;
pre[s] = -;
while(!que.empty()) que.pop();
dist[s] = ;
que.push(qnode(s, ));
qnode tmp;
while(!que.empty()) {
tmp = que.top();
que.pop();
int u = tmp.v;
if(vis[u]) continue;
vis[u] = true;
for(int i = ; i < E[u].size(); ++i) {
int v = E[u][i].v;
int cost = E[u][i].cost;
if(!vis[v] && dist[v] > dist[u] + cost) {
dist[v] = dist[u] + cost;
que.push(qnode(v, dist[v]));
pre[v] = u;
}
}
}
}
void addedge(int u, int v, int w) {
E[u].push_back(Edge(v, w));
E[v].push_back(Edge(u, w));
}
int main() {
#ifdef LOCAL
freopen("in.txt", "r", stdin);
#endif
int n, m, s, u, v, w, l;
scanf("%d%d%d", &n, &m, &s);
for(int i = ; i < m; ++i) {
scanf("%d%d%d", &u, &v, &w);
addedge(u, v, w);
}
scanf("%d", &l);
Dij(n, s);
int ans = ;
for(int i = ; i <= n; ++i) if(dist[i] == l) ans++;
int res = ;
for(int u = ; u <= n; ++u) {
for(int i = ; i < E[u].size(); ++i) {
int v = E[u][i].v;
int w = E[u][i].cost;
if(dist[u] < l && dist[u] + w > l && w + dist[u] + dist[v] >= * l) res++;
if(dist[v] < l && dist[v] + w > l && w + dist[v] + dist[u] >= * l) res++;
if(dist[u] < l && dist[v] < l && dist[u] + w > l && dist[v] + w > l && (l - dist[u] + l - dist[v] == w)) res--;
}
}
printf("%d\n", ans + res / );
return ;
}
Codeforces 144D Missile Silos 最短路的更多相关文章
- 最短路 Codeforces Round #103 (Div. 2) D. Missile Silos
题目传送门 /* 最短路: 不仅扫描边,还要扫描点:点有两种情况,一种刚好在中点,即从u,v都一样,那么最后/2 还有一种是从u,v不一样,两种的距离都是l 模板错了,逗了好久:( */ #inclu ...
- Codeforces Round #103 (Div. 2) D. Missile Silos(spfa + 枚举边)
题目链接:http://codeforces.com/problemset/problem/144/D 思路:首先spfa求出中心点S到其余每个顶点的距离,统计各顶点到中心点的距离为L的点,然后就是要 ...
- Codeforces 667D World Tour 最短路
链接 Codeforces 667D World Tour 题意 给你一个有向稀疏图,3000个点,5000条边. 问选出4个点A,B,C,D 使得 A-B, B-C, C-D 的最短路之和最大. 思 ...
- Codeforces 543B Destroying Roads(最短路)
题意: 给定一个n个点(n<=3000)所有边长为1的图,求最多可以删掉多少条边后,图满足s1到t1的距离小于l1,s2到t2的距离小于l2. Solution: 首先可以分两种情况讨论: 1: ...
- The Two Routes CodeForces - 601A(水最短路)
一个完全图 1和n肯定有一条路 不是公路就是铁路 另= 另一个跑遍最短路即可 #include <bits/stdc++.h> #define mem(a, b) memset(a, ...
- CodeForces - 938D-Buy a Ticket+最短路
Buy a Ticket 题意:有n个点和m条路(都收费),n个点在开演唱会,门票不同,对于生活在n个点的小伙伴,要求计算出每个小伙伴为了看一场演唱会要花费的最小价格: 思路: 这道题我一开始觉得要对 ...
- Codeforces 95C Volleyball(最短路)
题目链接:http://codeforces.com/problemset/problem/95/C C. Volleyball time limit per test 2 seconds memor ...
- Edge Deletion CodeForces - 1076D(水最短路)
题意: 设从1到每个点的最短距离为d,求删除几条边后仍然使1到每个点的距离为d,使得剩下的边最多为k 解析: 先求来一遍spfa,然后bfs遍历每条路,如果d[v] == d[u] + Node[i] ...
- Codeforces Beta Round #77 (Div. 1 Only) C. Volleyball (最短路)
题目链接:http://codeforces.com/contest/95/problem/C 思路:首先dijkstra预处理出每个顶点到其他顶点的最短距离,然后如果该出租车到某个顶点的距离小于等于 ...
随机推荐
- TCP学习之四:传输协议
参考学习张子阳大神的博客:http://www.cnblogs.com/JimmyZhang/category/101698.html 服务端接收到的数据可能是被截断或合并后的数据,需要协议来 ...
- SQL Server群集知识介绍
集群CLUSTER种类介绍 基于iSCSI的SQL Server 2012群集测试(一)--SQL群集安装 SQL Server群集如何在线检测 群集中的MS DTC分布式事务协调器 一.SQL Se ...
- nginx使用ngx_lua访问后端Thrift-Server实现和介绍
背景 随着openresty的出现,让nginx使用lua解决一些业务的能力大幅度提高,ngx_lua可以使用nginx自生的基于事件驱动的IO模型,和后端的存储,业务等系统实现非阻塞的连接交互. 如 ...
- C++ 简单字符串加解密(转载)
#include <iostream.h> #include <windows.h> #include <tchar.h> void EncodeString(LP ...
- 数据结构-浙大 MOOC 笔记一 基本概念
做一些笔记记录自己的学习过程 第一节课介绍了数据结构的基本概念,首先没有直接给出相关的定义而是通过思考如何在书架上摆放书籍这样一个简单的类比了解到数据的组织方式的重要性,并通过printN函数的循环实 ...
- Qt-导入第三方库
Qt提供了显式和隐式导入第三方库方法,本文只介绍显示导入方法. 第三方提供的库文件包括ControlCAN.h,ControlCAN.dll和ControlCAN.lib.将ControlCAN.h和 ...
- 读《Android编程权威指南》
因为去年双十二购买了一折的<Android 编程权威指南(第一版)>,在第二版出来后图灵社区给我推送了第二版的优惠码,激动之余就立马下单购买电子书,不得不说Big Nerd Ranch G ...
- 读一篇Javascript问题贴的收获
遇到这篇文章<Javascript异步调用时,回调函数内用到了函数外的变量>,是缘于我在<难道这就是JavaScript中的"闭包">文章中遇到的问题时,B ...
- 【Kubernetes】K8S网络方案--最近在看的
K8S网络-最近在看的 Create a Minikube cluster - Kubernetes Kubernetes Documentation - Kubernetes Kubernetes ...
- js_原型
原型是JavaScript中一个比较难理解的概念,原型相关的属性也比较多,对象有"prototype"属性,函数对象有"prototype"属性,原型对象有&q ...