• LOJ6252. 「CodePlus 2017 11 月赛」大吉大利,晚上吃鸡! 最短路+bitset


    题目传送门

    https://loj.ac/problem/6252

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

    题解

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

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

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

    我们把 (S o A o 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
    ", 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;
    }
    
  • 相关阅读:
    C++中派生类使用基类成员的问题
    关于linux安装kettle的总结
    Servlet+JSP教程之:第一个Web程序
    Oracle开启和关闭的四种模式
    Android 图片设置圆角 方法之二
    Hive[6] HiveQL 查询
    JavaScript技巧45招
    JavaScript 权威指南第6版
    js 使用技巧
    Hive[5] HiveQL 数据操作
  • 原文地址:https://www.cnblogs.com/hankeke/p/loj6252.html
Copyright © 2020-2023  润新知