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 ...
随机推荐
- 在中文windows下使用pywinauto进行窗口操作
这两天开始接触pywinauto,听说百度的自动化QA也用这个模块,于是来了兴趣,但网上的教程很少,而且基本上都是拿官方的notepad来说,首先中文菜单的支持是问题,其次各种操作也没有写清楚,阅读官 ...
- Unity3D研究院之拓展系统自带组件的Inspector视图
转自 http://www.xuanyusong.com/archives/3455 using UnityEngine; using System.Collections; using UnityE ...
- Delphi Excel 操作大全
Delphi Excel 操作大全 (一) 使用动态创建的方法首先创建 Excel 对象,使用ComObj:var ExcelApp: Variant;ExcelApp := CreateOleObj ...
- ACM/ICPC 之 BFS-简单障碍迷宫问题(POJ2935)
题目确实简单,思路很容易出来,难点在于障碍的记录,是BFS迷宫问题中很经典的题目了. POJ2935-Basic Wall Maze 题意:6*6棋盘,有三堵墙,求从给定初始点到给定终点的最短路,输出 ...
- ui router 介绍
1. 路由规则 rap框架页面路由基于ui-router实现 1.1 ui-router 一个基本的路由状态如下所示: 路由配置: $stateProvider .state('po',{ url:' ...
- 17. javacript高级程序设计-错误处理与调试
1. 错误处理与调试 l 在可能发生错误的地方使用try-catch方法,可以对错误进行及时的相应 l 使用window.onerror事件处理程序,这种方式可以接受try-catch不能处理的所有错 ...
- ffmpeg-20160629-git-bin.7z
ESC 退出 0 进度条开关 1 屏幕原始大小 2 屏幕1/2大小 3 屏幕1/3大小 4 屏幕1/4大小 S 下一帧 [ -2秒 ] +2秒 ; -1秒 ' +1秒 下一个帧 -> -5秒 f ...
- xcode报错,svn : is not a workingCopy
解决方案: 1.点击对应的targets 2.选择build phases 3.在红框处有一个run script选项(截图中已经删除了.),点击下拉按钮,看看是否是与svn有关的东西, 如果是,删除 ...
- Java IO流题库
一. 填空题 Java IO流可以分为 节点流 和处理流两大类,其中前者处于IO操作的第一线,所有操作必须通过他们进行. 输入流的唯一目的是提供通往数据的通道,程序可以通过这个通道读取数 ...
- No space left on device you must specify the filesystem type--Linux重启挂在失败
在Linux中拷贝了一个文件比较大5G,直接提示:No SPace Left On Device,很明显是磁盘空间不够了,我因为是在虚拟机上面建的,直接右击虚拟机==>编辑设置 如图片1所示, ...