CF144D Missile Silos SPFA最短路
来源:互联网 发布:淘宝怎么改最低折扣 编辑:程序博客网 时间:2024/05/23 15:42
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 11 2 11 3 32 3 12 4 13 4 11 4 22
3
5 6 33 1 13 2 13 4 13 5 11 2 64 5 84
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 <iostream>#include <stdio.h>#include <string.h>#include <algorithm>#include <queue>using namespace std;const int maxn=1111111;const int INF=0xfffffff;struct node{ int to; int w; int v; int next;}a[maxn];int n,m,s,l;int dis[maxn],head[maxn],vis[maxn];int edge;queue<int>q;void inint(){ edge=0; memset(head,-1,sizeof(head));}void addedge(int v,int u,int w){ a[edge].v=v; a[edge].to=u; a[edge].w=w; a[edge].next=head[v]; head[v]=edge++;}bool spfa(int s){ int v,i; while(!q.empty()) q.pop(); for(i=1;i<=n;i++) dis[i]=(i==s?0:INF); memset(vis,0,sizeof(vis)); vis[s]=1; q.push(s); while(!q.empty()) { int tmp=q.front(); q.pop(); for(i=head[tmp];i!=-1;i=a[i].next) { if(dis[tmp]+a[i].w<dis[v=a[i].to]) { dis[v]=dis[tmp]+a[i].w; if(!vis[v]) { vis[v]=true; q.push(v); } } } vis[tmp]=false; } return true;}int main(){ while(scanf("%d%d%d",&n,&m,&s)!=EOF) { int i; int v,u,w; inint(); for(i=0;i<m;i++) { scanf("%d%d%d",&v,&u,&w); addedge(v,u,w); addedge(u,v,w); } scanf("%d",&l); spfa(s); int ans=0; for(i=1;i<=n;i++) if(dis[i]==l) ans++; //cout<<ans<<endl; /*for(i=head[s];~i;i=a[i].next) { if(a[i].w>l) ans++; }*/ for(i=0;i<edge;i+=2) { v=a[i].v; u=a[i].to; w=a[i].w; if((dis[v]<l)&&(l-dis[v]<w)&&(w-(l-dis[v])>l-dis[u])) ans++; if((dis[u]<l)&&(l-dis[u]<w)&&(w-(l-dis[u])>l-dis[v])) ans++; if((dis[v]<l)&&(dis[u]<l)&&(dis[u]+dis[v]+w==l*2)) ans++; } cout<<ans<<endl; } return 0;}
- CF144D Missile Silos SPFA最短路
- Codeforces 144D Missile Silos(SPFA最短路)
- Codeforces 144 D Missile Silos【最短路SPFA+枚举】
- Codeforces 144D. Missile Silos 最短路
- codeforces 144 D. Missile Silos 最短路
- CF 144D Missile Silos [最短路+想法]
- codeforces 144D Missile Silos spfa
- Codeforces Round #103 (Div. 2)---D. Missile Silos(最短路+枚举边)
- codeforces D. Missile Silos
- Codeforces Round #103 (Div. 2) D. Missile Silos(spfa + 枚举边)
- Codeforces 144D: Missile Silos
- Codeforces 144D Missile Silos
- Codeforces 144D. Missile Silos【dijkstra】
- SPFA最短路
- 最短路之SPFA
- poj2472最短路spfa
- 最短路SPFA--poj3013
- 最短路 SPFA模板
- 妙趣横生的算法(1.1)动态创建顺序表
- hadoop,hbase,zookeeper安装配置
- 管理Oracle实例
- 位图BITMAP结构
- cocos2dx 2.1.4 程序运行脉络解析2—解析CCDirector
- CF144D Missile Silos SPFA最短路
- Yii 默认控制器 controller
- 我还是喜欢吉克隽逸
- 牛人分享的面试知识
- iOS7完美越狱教程
- Java 动态代理学习笔记
- Android中ListView与ArrayAdapter配合使用
- OpenCV基本函数学习
- 输入两个正整数X,Y,求出[X,Y]内被除3余1并且被除5余3的整数的和