题目链接: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. JQ添加标签

    <script type="text/javascript" src="http://files.cnblogs.com/914556495wxkj/jquery- ...

  2. struts2 校验数据的有效性 2种方式

    Struts2的数据校验: 数据的校验分为客户端校验和服务器端两种: 客户端校验:JS完成的校验.(为了提升用户体验.减少用户的输入错误) 服务器端校验:在后台的校验.(必须的.) 手动编码进行校验: ...

  3. linux (RHEL) 添加和删除用户

    linux添加新用户使用 useradd -----create a new user or update default new user information 删除用户使用userdel  -- ...

  4. ffmpeg-20160517-git-bin

    ESC 退出 0 进度条开关 1 屏幕原始大小 2 屏幕1/2大小 3 屏幕1/3大小 4 屏幕1/4大小 S 下一帧 [ -2秒 ] +2秒 ; -1秒 ' +1秒 下一个帧 -> -5秒 f ...

  5. 8.SpringMVC参数传递

    页面参数传递到controller, 可被同名(与页面标签上的name名对应)的参数接收,用request设值,页面再取出来. 注意乱码解决办法: ①如果是get提交,则在tomcat的server. ...

  6. gridview 实现鼠标悬浮行提示行中列的信息

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) {          for (int i = ...

  7. CCF 最大的矩形

    问题描述 试题编号: 3 试题名称: 最大的矩形 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 在横轴上放了n个相邻的矩形,每个矩形的宽度是1,而第i(1 ≤ i ≤ n)个 ...

  8. mvn命令备忘

    转换成eclipse项目mvn eclipse:eclipse 跳过testmvn install -Dmaven.test.skip=true mvn clean install -DskipTes ...

  9. Innodb之拷贝InnoDB表从一服务器到另一台服务器2

    本章节阐述将表移动到不同server上的技术.如可以将整个Mysql 实例转移到更大.更快的Server上:也可以拷贝整个实例到Slave上:也可以拷贝单个表或某些表到其他Server上(数据仓库). ...

  10. 创建DLL、Lib以及使用DLL、Lib

    1.要在生成DLL文件的同时生成Lib文件,函数声明时前面要加__declspec(dllexport). 可在头文件中如下定义: #ifndef __MYDLL_H#define __MYDLL_H ...