题目传送门

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. python测试redis是否可以使用

    前提打开redis服务,windows打开方式到redis的安装目录命令行输入redis-server from redis import StrictRedis redis = StrictRedi ...

  2. python web自动化测试框架搭建(功能&接口)——接口用例实现

    测试用例基类: # coding=utf-8 import unittest import Logger log = Logger.Loger() class BaseCase(unittest.Te ...

  3. (appium+python)UI自动化_03_元素定位工具

    前言 在UI自动化过程中,需要对手机app上的元素进行定位,然后进一步编写自动化脚本操作app.定位元素首先需要定位工具来辅助查看页面元素.小编常用的定位工具有2种,分别是uiautomatorvie ...

  4. python学习笔记:__init__.py的作用

    package标识,而非文件夹. 在pycharm中new,有Directory和Python Package两个选项. 分别创建Directory和package,发现前者只是一个空目录,后者包含一 ...

  5. C#递归加载目录树

    /// 获取目录管理信息集合 /// </summary> /// <returns></returns> public List<CatalogTree&g ...

  6. JavaScript.InjectedScriptHost

    "use strict"; (function(InjectedScriptHost, inspectedGlobalObject, injectedScriptId) {     ...

  7. Netty核心组件介绍及手写简易版Tomcat

    Netty是什么: 异步事件驱动框架,用于快速开发高i性能服务端和客户端 封装了JDK底层BIO和NIO模型,提供高度可用的API 自带编码解码器解决拆包粘包问题,用户只用关心业务逻辑 精心设计的Re ...

  8. 两数相加(java版本)

    (一).单链表实现 package com.lin.leetcode.addTwoNumbers; /** * Created by Yaooo on 2019/8/26. */ public cla ...

  9. bfs(标记整个棋盘)

    1004 四子连棋 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold       题目描述 Description 在一个4*4的棋盘上摆放了14颗棋子,其中有7颗白色 ...

  10. Yii中CreateUrl的使用总结

    在Yii中经常要生成URL,不管是为了自动跳转还是仅仅是一个链接.下面对Yii中的URL生成做了一个总结.提示:以下controllerX代表控制器X,actionX代表方法X.在Controller ...