LOJ6252. 「CodePlus 2017 11 月赛」大吉大利,晚上吃鸡! 最短路+bitset
题目传送门
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的更多相关文章
- 「CodePlus 2017 11 月赛」大吉大利,晚上吃鸡!(dij+bitset)
从S出发跑dij,从T出发跑dij,顺便最短路计数. 令$F(x)$为$S$到$T$最短路经过$x$的方案数,显然这个是可以用$S$到$x$的方案数乘$T$到$x$的方案数来得到. 然后第一个条件就变 ...
- 「CodePlus 2017 11 月赛」大吉大利,晚上吃鸡!
n<=50000,m<=50000的图,给s和t,问有多少点对$(a,b)$满足 嗯. 不会. 首先最短路DAG造出来,然后两个条件转述一下:条件一,$N_a$表示从s到t经过a的路径,$ ...
- loj #6250. 「CodePlus 2017 11 月赛」找爸爸
#6250. 「CodePlus 2017 11 月赛」找爸爸 题目描述 小 A 最近一直在找自己的爸爸,用什么办法呢,就是 DNA 比对. 小 A 有一套自己的 DNA 序列比较方法,其最终目标是最 ...
- [LOJ 6249]「CodePlus 2017 11 月赛」汀博尔
Description 有 n 棵树,初始时每棵树的高度为 H_i,第 i 棵树每月都会长高 A_i.现在有个木料长度总量为 S 的订单,客户要求每块木料的长度不能小于 L,而且木料必须是整棵树(即不 ...
- [LOJ 6248]「CodePlus 2017 11 月赛」晨跑
Description “无体育,不清华”.“每天锻炼一小时,健康工作五十年,幸福生活一辈子” 在清华,体育运动绝对是同学们生活中不可或缺的一部分.为了响应学校的号召,模范好学生王队长决定坚持晨跑.不 ...
- 「CodePlus 2017 11 月赛」Yazid 的新生舞会(树状数组/线段树)
学习了新姿势..(一直看不懂大爷的代码卡了好久T T 首先数字范围那么小可以考虑枚举众数来计算答案,设当前枚举到$x$,$s_i$为前$i$个数中$x$的出现次数,则满足$2*s_r-r > 2 ...
- 「CodePlus 2017 11 月赛」可做题
这种题先二进制拆位,显然改的位置只有每一段确定的数的开头和结尾,只需要对于每一个可决策位置都尝试一下填1和0,然后取min即可. #include<iostream> #include&l ...
- 「CodePlus 2017 11 月赛」Yazid 的新生舞会
n<=500000的数字,问有多少个区间的众数出现次数严格大于区间长度的一半. 这么说来一个区间就一个众数了,所以第一反应是枚举数字,对下标进行处理.然后没有第二反应.很好. 在枚举一个数字的时 ...
- [LOJ#6259]「CodePlus 2017 12 月赛」白金元首与独舞
[LOJ#6259]「CodePlus 2017 12 月赛」白金元首与独舞 试题描述 到河北省 见斯大林 / 在月光下 你的背影 / 让我们一起跳舞吧 うそだよ~ 河北省怎么可能有 Stalin. ...
随机推荐
- [CF959F]Mahmoud and Ehab and yet another xor task题解
搞n个线性基,然后每次在上一次的基础上插入读入的数,前缀和线性基,或者说珂持久化线性基. 然后一个num数组记录当时线性基里有多少数 然后每次前缀操作一下就珂以了 代码 #include <cs ...
- 【HDOJ6667】Roundgod and Milk Tea(模拟)
题意:有n个班级,每个班级有a[i]个人,b[i]杯奶茶 每个人至多喝一杯奶茶,且不能喝自己班的 问能喝到奶茶的最多总人数 n<=1e6,a[i],b[i]<=1e9 思路: 做法一: # ...
- DELPHI 把数据库中的数据转换成XML格式
function ReplaceString(AString: string): string; begin Result := StringReplace(AString, '&', '&a ...
- 《数据结构与算法(C语言版)》严蔚敏 | 第四章课本案例
//二叉树的顺序存储表示 #define MAXTSIZE 100 typedef TElemtype SqBiTree[MAXTSIZE]; SqBiTree bt; //二叉树的二叉链表存储表示 ...
- 学习日记14、EF 时间段查询
m_Rep.GetList(a => System.Data.Entity.DbFunctions.DiffDays(DateTime.Now, a.EndDate) < date); 命 ...
- mui初级入门教程(二)— html5+ webview 底部栏用法详解
文章来源:小青年原创发布时间:2016-05-19关键词:mui,html5+,webview转载需标注本文原始地址: http://zhaomenghuan.github.io/#!/blog/20 ...
- ElasticSearch ClusterBlockException[blocked by: [FORBIDDEN/12/index read-only / allow delete (api)]锁定状态,无法插入数据
PUT /twitter/_settings { "index.blocks.read_only_allow_delete": null } 官网给出的解决办法
- tr:hover变色的问题
做表格隔行变色(高亮显示),可以通过设置css中的 tr:hover伪类属性达到效果, 但是,会出一点小问题.td的背景色会覆盖tr的背景色, 在tr:hover下边加上一句:tr:hover td{ ...
- Linux_Bash脚本基础
目录 目录 Bash使用基础 if 语句 运算符 逻辑表达式 不将执行指令的结果显示出来 echo 语句 从Bash接受输入参数 Case语句 循环 for 语句 While语句 内置变量和函数 AW ...
- This service allows sftp connections only
这是因为该用用户只开通了sftp,ssh被禁了 可以通过别的主机ssh登陆这台机器 app@home:/software>ssh mysftp@192.168.0.1 Authorized on ...