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

26 篇文章 0 订阅
8 篇文章 0 订阅

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[N];
int n, m, S, T, x, y, z, Gcnt, clc;
vector<int> E[N], B[N];
priority_queue<Pairs> Q;
map<ll, bitset<N> > mp;
ll dis[N], sid[N];
int vis[N], onw[N];
ll f1[N], f2[N], f[N];
ll ans;
bitset<N> t1[N], t2[N], t[N];

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;
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值