您的位置:首页 > 其它

[最短路][DP][传递闭包] BZOJ 5109 && LOJ #6252. 「CodePlus 2017 11 月赛」大吉大利,晚上吃鸡!

2017-12-27 13:31 429 查看

Solution

记fu表示从S到T经过点u的方案数。

这可以很简单的DP出来。

那么点对(A,B)合法当且仅当

fA+fB=fT

A,B无法互相到达。

第一个用个表存,第二个按拓扑序做传递闭包就好了。

原来图会不连通

时间复杂度O(nlogn+nmw)

#include <bits/stdc++.h>
#include <assert.h>
using namespace std;

typedef long long ll;
typedef pair<ll, int> Pairs;
const ll INF = 1ll << 60;
const int N = 50505;
const int M = 50505;

inline char get(void) {
static char buf[100000], *S = buf, *T = buf;
if (S == T) {
T = (S = buf) + fread(buf, 1, 100000, stdin);
if (S == T) return EOF;
}
return *S++;
}
template<typename T>
inline void read(T &x) {
static char c; x = 0; int sgn = 0;
for (c = get(); c < '0' || c > '9'; c = get()) if (c == '-') sgn = 1;
for (; c >= '0' && c <= '9'; c = get()) x = x * 10 + c - '0';
if (sgn) x = -x;
}

struct edge {
int to, next;
ll key;
edge (int t = 0, int n = 0, ll k = 0):to(t), next(n), key(k) {}
};
edge G[M << 1];
int head
;
int n, m, S, T, x, y, z, Gcnt, clc;
vector<int> E
, B
;
priority_queue<Pairs> Q;
map<ll, bitset<N> > mp;
ll dis
, sid
;
int vis
, onw
;
ll f1
, f2
, f
;
ll ans;
bitset<N> t1
, t2
, t
;

inline void AddEdge(int from, int to, ll key) {
G[++Gcnt] = edge(to, head[from], key); head[from] = Gcnt;
G[++Gcnt] = edge(from, head[to], key); head[to] = Gcnt;
}
inline void Pre(int S, ll *dis) {
for (int i = 1; i <= n; i++) dis[i] = INF;
dis[S] = 0; Q.push(Pairs(0, S)); ++clc;
while (!Q.empty()) {
int u = Q.top().second; Q.pop();
if (vis[u] == clc) continue; vis[u] = clc;
for (int i = head[u]; i; i = G[i].next) {
if (vis[G[i].to] == clc) continue;
if (dis[G[i].to] > dis[u] + G[i].key) {
dis[G[i].to] = dis[u] + G[i].key;
Q.push(Pairs(-dis[G[i].to], G[i].to));
}
}
}
}
inline void dfs1(int u) {
t1[u][u] = 1;
if (vis[u] == clc) return;
vis[u] = clc;
for (int i = 0; i < B[u].size(); i++) {
int to = B[u][i];
dfs1(to); f1[u] += f1[to];
t1[u] |= t1[to];
}
}
inline void dfs2(int u) {
t2[u][u] = 1;
if (vis[u] == clc) return;
vis[u] = clc;
for (int i = 0; i < E[u].size(); i++) {
int to = E[u][i];
dfs2(to); f2[u] += f2[to];
t2[u] |= t2[to];
}
}

int main(void) {
freopen("7.in", "r", stdin);
freopen("1.out", "w", stdout);
read(n); read(m); read(S); read(T);
for (int i = 1; i <= m; i++) {
read(x); read(y); read(z);
AddEdge(x, y, z);
}
Pre(S, dis); Pre(T, sid);
if (dis[T] == INF) {
cout << (ll)n * (n - 1) / 2 << endl;
return 0;
}
for (int i = 1; i <= n; i++)
if (dis[i] + sid[i] == dis[T]) onw[i] = 1;
for (int i = 1; i <= n; i++)
if (onw[i])
for (int j = head[i]; j; j = G[j].next)
if (onw[G[j].to] && dis[G[j].to] == dis[i] + G[j].key) {
E[i].push_back(G[j].to);
B[G[j].to].push_back(i);
}
vis[S] = ++clc; f1[S] = 1; dfs1(T);
vis[T] = ++clc; f2[T] = 1; dfs2(S);
for (int i = 1; i <= n; i++) {
f[i] = f1[i] * f2[i];
t[i] = ~(t1[i] | t2[i]);
mp[f[i]].set(i);
}
for (int i = 1; i <= n; i++) {
if (!mp.count(f[T] - f[i])) continue;
ans += (mp[f[T] - f[i]] & t[i]).count();
}
cout << ans / 2 << endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: