Codeforces Round #103 (Div. 2) D. Missile Silos(spfa + 枚举边)
题目链接:http://codeforces.com/problemset/problem/144/D
思路:首先spfa求出中心点S到其余每个顶点的距离,统计各顶点到中心点的距离为L的点,然后就是要统计在边上的点了,可以枚举边(这里边的数量最多也就100000条),对于枚举的某条边,如果它的其中某个端点到S的距离记过这条边,也就是满足一下这个条件:d1 + w == d2 || d2 + w == d1,那么边上符合要求的点最多只有一个,否则,就要判断d1,d2的关系,对于求出的边上的某个符合要求的顶点,还要看对于另一端是否也符合最短路径的要求(一开始没考虑这个,wa了一发)。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#define REP(i, a, b) for (int i = (a); i < (b); ++i)
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
using namespace std; const int MAX_N = (100000 + 100);
int N, M, S, L, ans, dist[MAX_N];
int vis[MAX_N], flag[MAX_N]; struct Node {
int v, w;
Node() {}
Node(int _v, int _w) : v(_v), w(_w) {}
};
vector<Node > g[MAX_N]; struct Edge {
int u, v, w;
} edge[MAX_N << 1]; void spfa(int st)
{
memset(dist, 0x3f, sizeof(dist));
memset(vis, 0, sizeof(vis));
dist[st] = 0;
queue<int > que;
que.push(st);
while (!que.empty()) {
int u = que.front(); que.pop();
vis[u] = 0;
REP(i, 0, (int)g[u].size()) {
int v = g[u][i].v, w = g[u][i].w;
if (dist[u] + w < dist[v]) {
dist[v] = dist[u] + w;
if (!vis[v]) { vis[v] = 1; que.push(v); }
}
}
}
} int main()
{
while (cin >> N >> M >> S) {
FOR(i, 1, N) g[i].clear();
FOR(i, 1, M) {
int u, v, w; cin >> u >> v >> w;
g[u].push_back(Node(v, w));
g[v].push_back(Node(u, w));
edge[i].u = u, edge[i].v = v, edge[i].w = w;
}
cin >> L;
spfa(S);
memset(flag, 0, sizeof(flag));
ans = 0;
FOR(i, 1, N) if (dist[i] == L) ++ans;
FOR(i, 1, M) {
int d1 = dist[edge[i].u], d2 = dist[edge[i].v], w = edge[i].w;
if (d1 + w == d2 || d2 + w == d1) {
if ((d1 < L && d2 > L) || (d2 < L && d1 > L)) ++ans;
} else {
if (d1 < L && d1 + w > L){
int dd = d1 + w - L;
if (d2 + dd >= L) ++ans;
}
if (d2 < L && d2 + w > L) {
int dd = d2 + w - L;
if (d1 + dd >= L) ++ans;
}
if (2 * L == d1 + d2 + w) --ans;
}
}
cout << ans << endl;
}
return 0;
}
Codeforces Round #103 (Div. 2) D. Missile Silos(spfa + 枚举边)的更多相关文章
- 最短路 Codeforces Round #103 (Div. 2) D. Missile Silos
题目传送门 /* 最短路: 不仅扫描边,还要扫描点:点有两种情况,一种刚好在中点,即从u,v都一样,那么最后/2 还有一种是从u,v不一样,两种的距离都是l 模板错了,逗了好久:( */ #inclu ...
- Codeforces Round #394 (Div. 2) C. Dasha and Password —— 枚举
题目链接:http://codeforces.com/problemset/problem/761/C C. Dasha and Password time limit per test 2 seco ...
- Codeforces Round #552 (Div. 3) EFG(链表+set,dp,枚举公因数)
E https://codeforces.com/contest/1154/problem/E 题意 一个大小为n(1e6)的数组\(a[i]\)(n),两个人轮流选数,先找到当前数组中最大的数然后选 ...
- Codeforces Round #313 (Div. 2) A B C 思路 枚举 数学
A. Currency System in Geraldion time limit per test 2 seconds memory limit per test 256 megabytes in ...
- Codeforces Round #191 (Div. 2) A. Flipping Game【*枚举/DP/每次操作可将区间[i,j](1=<i<=j<=n)内牌的状态翻转(即0变1,1变0),求一次翻转操作后,1的个数尽量多】
A. Flipping Game time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces Round #451 (Div. 2) B. Proper Nutrition【枚举/扩展欧几里得/给你n问有没有两个非负整数x,y满足x·a + y·b = n】
B. Proper Nutrition time limit per test 1 second memory limit per test 256 megabytes input standard ...
- Codeforces Round #633 (Div. 2)
Codeforces Round #633(Div.2) \(A.Filling\ Diamonds\) 答案就是构成的六边形数量+1 //#pragma GCC optimize("O3& ...
- Codeforces Round #366 (Div. 2) ABC
Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
随机推荐
- 【leetcode】Minimum Path Sum
Minimum Path Sum Given a m x n grid filled with non-negative numbers, find a path from top left to b ...
- Python之virtualenv安装
CentOS 7 yum install python-virtualenv virtualenv --no-site-packages testenv #不依赖真实环境的packages用 --no ...
- ios NSURLSession completeHandler默认调用quque
注意 , [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSU ...
- SAP打印出库单 新需求
*&---------------------------------------------------------------------* *& Report Z_SD_CKD ...
- 初次使用 VUX
1. 因为以前没用过vux ,所以还是比较不熟练: 2.项目部署是根据git上的源码改的: 开始:将git上的项目下载或者clone到本地: 001:安装nodejs--> 002:npm in ...
- jquery中使用event.target的几点
jquery中使用event.target的几点 1.this和event.target的区别: js中事件是会冒泡的,所以this是可以变化的,但event.target不会变化,它永远是直接接受事 ...
- 【轮子】发现一个效果丰富酷炫的Android动画库
没有什么比发现一个好轮子更让人开心的了. 这个库分分钟提高交互体验 :AndroidViewAnimations 一张图说明一切 配置和使用也相当简单 GitHub地址
- osgconv 批量转换
@echo offfor /f "delims=" %%i in ('dir/b *.osg') do ( "osgconv.exe" "%%~ni. ...
- nodejs链接mongodb数据库
nodeJs链接mongodb数据库有两种方式,第一种是利用官方自己开发的npm包mongodb链接,第二种是利用第三方npm包mongoose链接:这里如果是window操作系统,建议用mongoo ...
- Express 4 中如何使用connect-mongo
正在跟随上面的教程一步一步做,在会话支持那一节中安装 connect-mongo 后,添加: var MongoStore = require('connect-mongo')(express); v ...