题目链接: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 + 枚举边)的更多相关文章

  1. 最短路 Codeforces Round #103 (Div. 2) D. Missile Silos

    题目传送门 /* 最短路: 不仅扫描边,还要扫描点:点有两种情况,一种刚好在中点,即从u,v都一样,那么最后/2 还有一种是从u,v不一样,两种的距离都是l 模板错了,逗了好久:( */ #inclu ...

  2. 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 ...

  3. Codeforces Round #552 (Div. 3) EFG(链表+set,dp,枚举公因数)

    E https://codeforces.com/contest/1154/problem/E 题意 一个大小为n(1e6)的数组\(a[i]\)(n),两个人轮流选数,先找到当前数组中最大的数然后选 ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. Codeforces Round #633 (Div. 2)

    Codeforces Round #633(Div.2) \(A.Filling\ Diamonds\) 答案就是构成的六边形数量+1 //#pragma GCC optimize("O3& ...

  8. 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 ...

  9. Codeforces Round #354 (Div. 2) ABCD

    Codeforces Round #354 (Div. 2) Problems     # Name     A Nicholas and Permutation standard input/out ...

随机推荐

  1. C++中string,wstring,CString的基本概念和用法

    一.概念 string和CString均是字符串模板类,string为标准模板类(STL)定义的字符串类,已经纳入C++标准之中.wstring是操作宽字符串的类.C++标准程序库对于string的设 ...

  2. 使用Java的多线程和IO流写一个文件复制功能类

    创建一个复制功能类,继承Thread类,重写run()方法,把FileInputStream和FileOutputStream输入输出流写在run()方法内.示例代码如下: import java.i ...

  3. windows hosts

    2015年6月29日 11:10:56 星期一 windows 的 hosts 文件生效机制 以最前边的为准, 重复的硬解析只有第一次出现的地方生效

  4. 用基础动画实现iOS控件循环旋转

    - (void)viewDidLoad { [super viewDidLoad]; UIButton* ag=[[UIButton alloc]initWithFrame:CGRectMake(sc ...

  5. (转)JAVA AJAX教程第二章-JAVASCRIPT基础知识

    开篇:JAVASCRIPT是AJAX技术中不可或缺的一部分,所以想学好AJAX以及现在流行的AJAX框架,学好JAVASCRIPT是最重要的.这章我给大家整理了一些JAVASCRIPT的基础知识.常用 ...

  6. WPS文字在表格中打字自动跳动

    可以设置表格的属性来实现. 1.选择表格,点击鼠标右键,选择“表格属性” 2.在出现的对话框中,文字环绕选择“无”,“行”的设置为“允许跨页断行”,就可以了.

  7. Target runtime Apache Tomcat v6.0 is not defined.错误解决方法

    一.背景 最近在使用本地的tomcat进行运行项目的时候,发现出现了如题所述的问题.不知道什么原因,经过努力解决了该问题. 二.解决步骤 右击项目---选择属性---选择targeted runtim ...

  8. [Android Pro] InputStream.skip方法的思考

    参考 : http://blog.csdn.net/gsyzhu/article/details/8102286 在java.io.InputStream类中定义了skip这个方法.在API中的描述如 ...

  9. NYOJ之Binary String Matching

    Binary String Matching 时间限制:3000 ms  |  内存限制:65535 KB 难度:3 描述     Given two strings A and B, whose a ...

  10. Jmeter 中通过(_time函数)获取10位时间戳的方法

    meter的__time函数作用是取当前时间的时间戳,默认取的时间精确到了毫秒级别,所以获取的时间戳默认是13位的.  下图为取10位的时间戳的函数表达式(时间精确到秒)