题目传送门

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. 科匠中国深圳java面试笔试题

  2. django缓存优化(一)

    在配置之前,先介绍一个实用的工具: 当我们进入虚拟环境,在shell中进行操作的时候,往往要导入django的各种配置文件: from django.x import xxxx 这时我们可以借助dja ...

  3. [WCF REST] WebServiceHost 不依赖配置文件启动简单服务

    最近用WPF启动 WCF REST 服务,发现app.config 配置好烦,简单一个exe 可以到处搬动,还非得带一个累赘配置,不小心丢了程序就跑不起来. 最后决定,砍去WCF配置项,用WebSer ...

  4. NuGet-Doc:NuGet.Server

    ylbtech-NuGet-Doc:NuGet.Server 1.返回顶部 1. NuGet.Server 2018/03/13 NuGet.Server 是由 .NET Foundation 提供的 ...

  5. PHP-图片处理

    开启 GD 扩展(php_gd2.dll) 创建画布 画布:一种资源型数据,可以操作的图像资源. 创建新画布(新建) ImageCreate(宽,高); 创建基于调色板的画布. imageCreate ...

  6. Vue过渡:JavaScript钩子

    一 App.vue <template> <div id="app"> <button @click="show = !show" ...

  7. 关于static以及final关键字

    Static关键字: 可以用来修饰类中的属性.类中的方法.以及具体的某一个类. 1.用于修饰属性: 则表示该属性属于整个类,不论有多少个对象实例,所有的实例共同拥有一个static静态的成员变量.该变 ...

  8. Ajax表单提交给C#后台选中的checkbox值

    HTML页面: <input name="payWay" type="checkbox" value="1" />  <i ...

  9. 微信小程序这一块(下)

    1.小程序分为两部分 :页面.组件 2.小程序中如何定义自定义组件? 凡是通过Page创建的都是页面,而通过Components创建的都是组件 当组件创建成功后,在需要使用的页面进行引入,(找到页面的 ...

  10. JSP基础--三大指令

    JSP指令 1       JSP指令概述 JSP指令的格式:<%@指令名 attr1=”” attr2=”” %>,一般都会把JSP指令放到JSP文件的最上方,但这不是必须的. JSP中 ...