题目传送门

https://loj.ac/problem/6252

https://lydsy.com/JudgeOnline/problem.php?id=5109

题解

首先跑最短路,只保留 \(dis[v] = dis[u] + w\) 的边,形成一个 DAG。

如果只有一个点的话,如何判断这个点是否是必经之点。一个很简单的方式是判断 \(S\to A \to T\) 的方案数是否等于 \(S\to T\) 的方案数。

但是这里的要求是两个点,那么就是 \(S\to A \to T\) 的方案加上 \(S\to B \to T\) 的方案,再减去 \(S\to A\to B \to T\) 的方案,等于 \(S\to T\) 的方案。但是由于必须满足不存在一条路径同时经过 \(AB\),所以不需要减去,但是需要保证 \(A\) 和 \(B\) 不能相互到达。

我们把 \(S\to A\to B\) 的方案记为 \(f(A)\),则必须 \(f(A) + f(B) = f(T)\)。但是需要保证 \(A\) 和 \(B\) 不能相互到达,这个可以通过 bitset 来维护对于一个点,哪些点无法和它相互到达就可以了。同时维护 \(b[x]\) 为 \(f(A) = x\) 的 \(A\) 点的集合,与之前的那个 bitset 取交就可以了。


下面是代码,时间复杂度 \(O(\frac{n^2}{64})\),空间复杂度 \(O(\frac{n^2}{8})\)。由于空间复杂度太大,在 bzoj 上是在过不去,在 loj 上可以 AC。

#include<bits/stdc++.h>
#include<tr1/unordered_map> #define fec(i, x, y) (int i = head[x], y = g[i].to; i; i = g[i].ne, y = g[i].to)
#define dbg(...) fprintf(stderr, __VA_ARGS__)
#define File(x) freopen(#x".in", "r", stdin), freopen(#x".out", "w", stdout)
#define fi first
#define se second
#define pb push_back template<typename A, typename B> inline char smax(A &a, const B &b) {return a < b ? a = b , 1 : 0;}
template<typename A, typename B> inline char smin(A &a, const B &b) {return b < a ? a = b , 1 : 0;} typedef long long ll; typedef unsigned long long ull; typedef std::pair<ll, int> pii; template<typename I>
inline void read(I &x) {
int f = 0, c;
while (!isdigit(c = getchar())) c == '-' ? f = 1 : 0;
x = c & 15;
while (isdigit(c = getchar())) x = (x << 1) + (x << 3) + (c & 15);
f ? x = -x : 0;
} const int N = 5e4 + 7;
const int P = 998244353; int n, m, S, T, tl;
int vis[N], deg[N], dq[N], f1[N], f2[N], f[N];
ll dis[N];
std::priority_queue<pii, std::vector<pii>, std::greater<pii> > q;
std::bitset<N> b1[N], b2[N];
std::tr1::unordered_map<int, std::bitset<N> > b3; inline int smod(int x) { return x >= P ? x - P : x; }
inline void sadd(int &x, const int &y) { x += y; x >= P ? x -= P : x; }
inline int fpow(int x, int y) {
int ans = 1;
for (; y; y >>= 1, x = (ll)x * x % P) if (y & 1) ans = (ll)ans * x % P;
return ans;
} struct Edge { int to, ne, w; } g[N << 1]; int head[N], tot;
inline void addedge(int x, int y, int z) { g[++tot].to = y, g[tot].w = z, g[tot].ne = head[x], head[x] = tot; }
inline void adde(int x, int y, int z) { addedge(x, y, z), addedge(y, x, z); } inline void dijkstra() {
memset(dis, 0x3f, sizeof(dis)), dis[S] = 0;
q.push(pii(dis[S], S));
while (!q.empty()) {
int x = q.top().se; q.pop();
if (vis[x]) continue;
vis[x] = 1;
if (x == T) return;
for fec(i, x, y) if (smin(dis[y], dis[x] + g[i].w)) q.push(pii(dis[y], y));
}
} inline void bfs() {
int hd = 0;
for (int x = 1; x <= n; ++x) for fec(i, x, y) if (dis[y] == dis[x] + g[i].w) ++deg[y];
for (int i = 1; i <= n; ++i) if (!deg[i]) dq[++tl] = i;
while (hd < tl) {
int x = dq[++hd];
for fec(i, x, y) if (dis[y] == dis[x] + g[i].w && !--deg[y]) dq[++tl] = y;
}
} inline void dp() {
f1[S] = f2[T] = 1;
for (int i = 1; i <= tl; ++i) {
int x = dq[i];
b1[x].set(x);
for fec(i, x, y) if (dis[x] == dis[y] + g[i].w) sadd(f1[x], f1[y]), b1[x] |= b1[y];
}
for (int i = tl; i; --i) {
int x = dq[i];
b2[x].set(x);
for fec(i, x, y) if (dis[y] == dis[x] + g[i].w) sadd(f2[x], f2[y]), b2[x] |= b2[y];
}
for (int i = 1; i <= n; ++i) f[i] = (ll)f1[i] * f2[i] % P, b1[i] |= b2[i], b1[i] = ~b1[i], b3[f[i]].set(i);
for (int i = 1; i <= n; ++i) {
if (f[i] == f[T]) b1[i] |= b3[0];
if (f[i] == 0) b1[i].reset().flip();
b1[i].set(i, 0);
}
} inline void work() {
dijkstra();
bfs();
dp();
ll ans = 0;
for (int i = 1; i <= n; ++i) if (b3.count(smod(P + f[T] - f[i]))) ans += (b3[smod(P + f[T] - f[i])] & b1[i]).count();
printf("%lld\n", ans / 2);
} inline void init() {
read(n), read(m), read(S), read(T);
int x, y, z;
for (int i = 1; i <= m; ++i) read(x), read(y), read(z), adde(x, y, z);
} int main() {
#ifdef hzhkk
freopen("hkk.in", "r", stdin);
#endif
init();
work();
fclose(stdin), fclose(stdout);
return 0;
}

LOJ6252. 「CodePlus 2017 11 月赛」大吉大利,晚上吃鸡! 最短路+bitset的更多相关文章

  1. 「CodePlus 2017 11 月赛」大吉大利,晚上吃鸡!(dij+bitset)

    从S出发跑dij,从T出发跑dij,顺便最短路计数. 令$F(x)$为$S$到$T$最短路经过$x$的方案数,显然这个是可以用$S$到$x$的方案数乘$T$到$x$的方案数来得到. 然后第一个条件就变 ...

  2. 「CodePlus 2017 11 月赛」大吉大利,晚上吃鸡!

    n<=50000,m<=50000的图,给s和t,问有多少点对$(a,b)$满足 嗯. 不会. 首先最短路DAG造出来,然后两个条件转述一下:条件一,$N_a$表示从s到t经过a的路径,$ ...

  3. loj #6250. 「CodePlus 2017 11 月赛」找爸爸

    #6250. 「CodePlus 2017 11 月赛」找爸爸 题目描述 小 A 最近一直在找自己的爸爸,用什么办法呢,就是 DNA 比对. 小 A 有一套自己的 DNA 序列比较方法,其最终目标是最 ...

  4. [LOJ 6249]「CodePlus 2017 11 月赛」汀博尔

    Description 有 n 棵树,初始时每棵树的高度为 H_i,第 i 棵树每月都会长高 A_i.现在有个木料长度总量为 S 的订单,客户要求每块木料的长度不能小于 L,而且木料必须是整棵树(即不 ...

  5. [LOJ 6248]「CodePlus 2017 11 月赛」晨跑

    Description “无体育,不清华”.“每天锻炼一小时,健康工作五十年,幸福生活一辈子” 在清华,体育运动绝对是同学们生活中不可或缺的一部分.为了响应学校的号召,模范好学生王队长决定坚持晨跑.不 ...

  6. 「CodePlus 2017 11 月赛」Yazid 的新生舞会(树状数组/线段树)

    学习了新姿势..(一直看不懂大爷的代码卡了好久T T 首先数字范围那么小可以考虑枚举众数来计算答案,设当前枚举到$x$,$s_i$为前$i$个数中$x$的出现次数,则满足$2*s_r-r > 2 ...

  7. 「CodePlus 2017 11 月赛」可做题

    这种题先二进制拆位,显然改的位置只有每一段确定的数的开头和结尾,只需要对于每一个可决策位置都尝试一下填1和0,然后取min即可. #include<iostream> #include&l ...

  8. 「CodePlus 2017 11 月赛」Yazid 的新生舞会

    n<=500000的数字,问有多少个区间的众数出现次数严格大于区间长度的一半. 这么说来一个区间就一个众数了,所以第一反应是枚举数字,对下标进行处理.然后没有第二反应.很好. 在枚举一个数字的时 ...

  9. [LOJ#6259]「CodePlus 2017 12 月赛」白金元首与独舞

    [LOJ#6259]「CodePlus 2017 12 月赛」白金元首与独舞 试题描述 到河北省 见斯大林 / 在月光下 你的背影 / 让我们一起跳舞吧 うそだよ~ 河北省怎么可能有 Stalin. ...

随机推荐

  1. C#通过文件头判断文件的类型(不是后缀名)

    FileStream fs=new FileStream(@"D:\6",FileMode.Open,FileAccess.Read); BinaryReader reader= ...

  2. 向量积&&凸包算法

    参考:Thanks 百度百科 http://blog.csdn.net/keng_s/article/details/52131034 https://www.cnblogs.com/aiguona/ ...

  3. 【HDOJ6686】Rikka with Travels(树形DP)

    题意:给定一棵n个点,边权为1的树,求有多少个有序数对(l1,l2)使得存在两条互不相交的路径,长度分别为l1和l2 n<=1e5 思路: #include<bits/stdc++.h&g ...

  4. Sublime Text3 代码编辑器使用笔记

    Sublime Text3 作为一款代码的文本编辑器,有许多插件,这一点是我认为 Sublime Text3 很强大的原因之一.插件的安装可以参考下面的文章. Sublime Text3 插件安装教程 ...

  5. HTML5 新属性的讲解

    1.选择器: 标签选择器: class选择器: id选择器: 后代选择器:div li div下所有li 子代选择器:div>li div的所有子一代 li 元素 交集选择器:div.class ...

  6. Cadence 学习

    记录学习Cadence的资料 Cadence 16.6软件             链接: http://pan.baidu.com/s/1mgwSeYs 密码: jemk 于博士视频教程(15.7版 ...

  7. 前端工具-gulp-ruby-sass-解决带有中文路径报错(incompatible character encodings GBK and UTF-8)

    注意:错误提示真的是非常重要的!!! 今天 gulp 一个外国人的项目时编译 sass 时提示 Encoding::CompatibilityError: incompatible character ...

  8. 将原生JS和jquery里面的DOM操作进行了一下整理

    创建元素节点 1.原生: document.createElement("div") 2.jquery: $("<div></div>" ...

  9. 大数据学习笔记之Zookeeper(三):Zookeeper理论篇(二)

    文章目录 3.1 数据结构 3.2 节点类型 3.3 特点 3.4 选举机制 3.5 stat结构体 3.6 监听器原理 3.1 数据结构 ZooKeeper数据模型的结构与Unix文件系统很类似,整 ...

  10. redis连接报错:MISCONF Redis is configured to save RDB snapshots, but it is currently not able to...

    连接redis报错: MISCONF Redis is configured to save RDB snapshots, but it is currently not able to persis ...