您的位置:首页 > 其它

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,

#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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: