codeforces 144D Missile Silos(最短路)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud
Missile Silos
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 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.
题意:
给出一张图,问图上所有到s点的距离为d的有几个点。
一遍最短路,得到s到所有点的最短距离。然后在枚举每条边,统计边上是否有满足要求的点。
![](/assets/blank.gif)
![](/assets/blank.gif)
1 #include <iostream> 2 #include <sstream> 3 #include <ios> 4 #include <iomanip> 5 #include <functional> 6 #include <algorithm> 7 #include <vector> 8 #include <string> 9 #include <list> 10 #include <queue> 11 #include <deque> 12 #include <stack> 13 #include <set> 14 #include <map> 15 #include <cstdio> 16 #include <cstdlib> 17 #include <cmath> 18 #include <cstring> 19 #include <climits> 20 #include <cctype> 21 using namespace std; 22 #define XINF INT_MAX 23 #define INF 0x3FFFFFFF 24 #define MP(X,Y) make_pair(X,Y) 25 #define PB(X) push_back(X) 26 #define REP(X,N) for(int X=0;X<N;X++) 27 #define REP2(X,L,R) for(int X=L;X<=R;X++) 28 #define DEP(X,R,L) for(int X=R;X>=L;X--) 29 #define CLR(A,X) memset(A,X,sizeof(A)) 30 #define IT iterator 31 typedef long long ll; 32 typedef pair<int,int> PII; 33 typedef vector<PII> VII; 34 typedef vector<int> VI; 35 #define MAXN 100100 36 vector<PII> Map[MAXN]; 37 38 //清空邻接表 39 void init() { REP(i,MAXN) Map[i].clear(); } 40 41 //求以s为源点的最短路 结果保存在dis中 42 int dis[MAXN]; 43 void dijkstra(int s) 44 { 45 REP(i,MAXN){dis[i]=i==s?0:INF;} 46 int vis[MAXN] = {0}; 47 priority_queue<PII, vector<PII>, greater<PII> > q; 48 q.push(MP(0,s)); 49 while(!q.empty()) 50 { 51 PII p = q.top(); q.pop(); 52 int x = p.second; 53 if(vis[x])continue; 54 vis[x] = 1; 55 for(vector<PII>::iterator it = Map[x].begin(); it != Map[x].end(); it++) 56 { 57 int y = it->first; 58 int d = it->second; 59 if(!vis[y] && dis[y] > dis[x] + d) 60 { 61 dis[y] = dis[x] + d; 62 q.push(MP(dis[y],y)); 63 } 64 } 65 } 66 } 67 68 struct node 69 { 70 int u,v,d; 71 }edge[MAXN]; 72 int main() 73 { 74 ios::sync_with_stdio(false); 75 int n,m,s; 76 while(cin>>n>>m>>s) 77 { 78 int u,v,d; 79 init(); 80 for(int i=0;i<m;i++) 81 { 82 cin>>u>>v>>d; 83 u--; 84 v--; 85 Map[u].PB(MP(v,d)); 86 Map[v].PB(MP(u,d)); 87 edge[i].u=u; 88 edge[i].v=v; 89 edge[i].d=d; 90 } 91 int l; 92 cin>>l; 93 s--; 94 dijkstra(s); 95 int ans=0; 96 for(int i=0;i<n;i++) 97 { 98 if(dis[i]==l)ans++; 99 } 100 for(int i=0;i<m;i++) 101 { 102 u=edge[i].u; 103 v=edge[i].v; 104 d=edge[i].d; 105 if(dis[u]>dis[v])swap(u,v); 106 if(dis[v]-dis[u]==d) 107 { 108 if(l>dis[u]&&l<dis[v])ans++; 109 } 110 else 111 { 112 int x=l-dis[u]; 113 if(x<=0)continue; 114 if(x>d)continue; 115 if(dis[v]>l&&x<d) 116 { 117 ans++; 118 continue; 119 } 120 if(dis[v]==l&&x<d) 121 { 122 ans++; 123 continue; 124 } 125 int y=l-dis[v]; 126 if(x+y==d) 127 { 128 ans++; 129 continue; 130 } 131 if(x<d-y)ans++; 132 if(y<d-x)ans++; 133 } 134 } 135 cout<<ans<<endl; 136 } 137 138 139 return 0; 140 }
代码君
转载于:https://www.cnblogs.com/fraud/p/4338521.html
codeforces 144D Missile Silos(最短路)相关推荐
- Codeforces 144D. Missile Silos 最短路
D. Missile Silos time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- Codeforces 144D: Missile Silos
链接:http://codeforces.com/problemset/problem/144/D 题意:题目给你一个无向边权图,然后给你一个点s,和长度l,问距离点s长度为l的点有多少,这些点可以在 ...
- codeforces 144D Missile Silos spfa
题意:给定一张地图,可以在边和点上设立发射井,其中发射井到首都的距离必须等于l 做法:十万个点,竟然可以求最短路,而且最后时间还只有区区125ms.可能是边太少的原因... 图中的边可以分成两类,一种 ...
- CF 144D Missile Silos [最短路+想法]
题意: 给出一张图和图上的一个顶点,求距离这个点距离为s(最短距离)的顶点或边上的点总共有几个(边上的点要保证也是最短距离) 分析: 先用DIJ求出最短路 然后对所有顶点,距离为s的点都算上 枚举每条 ...
- 【CodeForces - 144D】Missile Silos(单源最短路,枚举中间边,枚举情况可能性)
题干: A country called Berland consists of n cities, numbered with integer numbers from 1to n. Some of ...
- codeforces144——D. Missile Silos(最短路+枚举)
codeforces144--D. Missile Silos 原题链接 题意: 给定一个n点m边的无向图,给定起点,求和起点最短距离为l的点有多少个(可以是点也可以在边上) 思路: 首先求一遍最短路 ...
- CF144D Missile Silos 解题报告 *
CF144D Missile Silos 解题报告 1 题目链接 https://codeforces.com/problemset/problem/144/D 2 题目整理 题目 : 导弹发射井 题 ...
- Missile Silos CodeForces - 144D
原题链接 考察:dijkstra 思路: 可以证明一定不存在dist[u]+w1(w1>w/2) = d(该点离v更近.)的情况. #include <iostream> #incl ...
- CodeForces - 1484F Useful Edges(最短路)
题目链接:点击查看 题目大意:给出由 nnn 个点构成的无向图,再给出 qqq 个三元对 (u,v,l)(u,v,l)(u,v,l),现在问有多少条边 (i,j)(i,j)(i,j) 可以和至少一个三 ...
最新文章
- 因为一条SQL,程序员差点被祭天......
- 滴滴自动驾驶,现在是一个怎样的“富二代”创业项目?
- Building and running Node.js for Android
- node创建web静态服务
- 暴雪应该从《争霸艾泽拉斯》中吸取什么教训?
- 前端学习(1908)vue之电商管理系统电商系统之渲染修改用户的表单预先验证
- python beautifulsoup4_Python之Beautiful Soup 4使用实例
- 无法进入页面,且浏览器调试界面->Timing报CAUTION:request is not finished yet!
- Codeforces Round #575 (Div. 3) 题解
- 通过ffmpeg将FLV文件转换为MP4
- 传智播客-刘意-java深入浅出精华版学习笔记Day02
- 我看过的安全方面的好文章
- linux alsa 音量参数
- 记某淘宝客软件分析拿库思路
- 用java判断三角形类型_判断三角形类型
- 以人为本 体验至上(三)
- 如何对需求进行排序?
- 3dmax卡顿、卡死 怎么解决?
- IvParameterSpec 干什么用的
- ImportError: DLL load failed while importing win32api