Codeforces 144D Missile Silos 最短路
2016-07-27 15:32
435 查看
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,
View Code
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,
#include <bits/stdc++.h> using namespace std; const int INF = 0x3f3f3f3f; typedef long long ll; const int N = 1e5 + 10; struct qnode { int v; int c; qnode(int _v = 0, int _c = 0) : v(_v), c(_c) { } bool operator < (const qnode &r) const { return c > r.c; }; }; struct Edge { int v, cost; Edge(int _v = 0, int _cost = 0) : v(_v), cost(_cost) { } }; vector<Edge> E ; bool vis ; int dist , pre ; void Dij(int n, int s) { memset(vis, false, sizeof vis); for(int i = 1; i <= n; ++i) dist[i] = INF; priority_queue<qnode> que; pre[s] = -1; while(!que.empty()) que.pop(); dist[s] = 0; que.push(qnode(s, 0)); qnode tmp; while(!que.empty()) { tmp = que.top(); que.pop(); int u = tmp.v; if(vis[u]) continue; vis[u] = true; for(int i = 0; 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 = 0; i < m; ++i) { scanf("%d%d%d", &u, &v, &w); addedge(u, v, w); } scanf("%d", &l); Dij(n, s); int ans = 0; for(int i = 1; i <= n; ++i) if(dist[i] == l) ans++; int res = 0; for(int u = 1; u <= n; ++u) { for(int i = 0; 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] >= 2 * l) res++; if(dist[v] < l && dist[v] + w > l && w + dist[v] + dist[u] >= 2 * 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 / 2); return 0; }
View Code
相关文章推荐
- Linux学习
- linux 系统 eclipse提交job到hadoop集群上的一些坑
- nitification简单使用
- html5的一些新增标签。。
- RxJava(三)简单常用操作符
- iOS 封装SDK
- LAMP平台部署及应用一
- javascript 引用类型 - Function类型(特性)
- myeclipse 10的破解以及运行run.bat错误或者双击立即消失的问题
- python--gevent协程及协程概念
- maven编译javafx找不到
- Android提醒Dialog、Toast和Snackbar ---站在巨人的肩膀上学习总结
- document.ready和onload的区别
- Convert dictionary values into array
- 自定义view的回调函数
- VC++编程实现修改EXE文件图标
- Cow Bowling
- 商场内自动扶梯的研究
- java 读取大文件
- IIS下架设SSL的艰辛历程