# [最短路图] Codeforces 1650G

## Code

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

#define LL long long

template<typename elemType>
elemType X = 0, w = 0; char ch = 0;
while (!isdigit(ch)) { w |= ch == '-';ch = getchar(); }
while (isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
T = (w ? -X : X);
}

const int maxn = 200010;
const LL MOD = 1e9 + 7;

int T, n, m, s, t;

struct Graph {
struct edge { int Next, to, w; };
edge G[maxn << 1];
int cnt;

Graph() :cnt(2) {}
void clear(int n) {
}
void add_edge(int u, int v) {
G[cnt].w = 0;
G[cnt].to = v;
}
};
Graph G;

struct Node { int Value, ID; };
struct cmpA { bool operator()(Node A, Node B) { return A.Value > B.Value; } };
priority_queue<Node, vector<Node>, cmpA> Q;
int dis[maxn];
bool vis[maxn];

void Dijkstra(int st) {
for (int i = 1;i <= n;++i) {
dis[i] = 0x3f3f3f3f;
vis[i] = false;
}
dis[st] = 0;Q.push((Node) { 0, st });
while (!Q.empty()) {
int u = Q.top().ID;Q.pop();
if (vis[u]) continue;
vis[u] = true;
for (int i = G.head[u];i;i = G.G[i].Next) {
int v = G.G[i].to;
if (vis[v]) continue;
if (dis[v] > dis[u] + 1) {
dis[v] = dis[u] + 1;
Q.push((Node) { dis[v], v });
}
}
}
}

void build_spg() {
for (int u = 1;u <= n;++u) {
for (int i = G.head[u];i;i = G.G[i].Next) {
int v = G.G[i].to;
if (dis[u] + 1 == dis[v])
G.G[i].w = G.G[i ^ 1].w = 1;
}
}
}

LL dp_s[maxn], dp_t[maxn];
queue<int> Que;

void calc_dp(int st, LL* dp, int opt) {
for (int i = 1;i <= n;++i) {
vis[i] = false;
dp[i] = 0;
}
dp[st] = 1; vis[st] = true; Que.push(st);
while (!Que.empty()) {
int u = Que.front(); Que.pop();
for (int i = G.head[u];i;i = G.G[i].Next) {
if (G.G[i].w == 0) continue;
int v = G.G[i].to;
if (dis[v] * opt > dis[u] * opt) dp[v] = (dp[v] + dp[u]) % MOD;
if (!vis[v]) { vis[v] = true; Que.push(v); }
}
}
}

int main() {
while (T--) {
G.clear(n);
for (int i = 1;i <= m;++i) {
int u, v;
}
Dijkstra(s);
build_spg();
calc_dp(s, dp_s, 1);
calc_dp(t, dp_t, -1);
LL ans = 0;
for (int u = 1;u <= n;++u) {
for (int i = G.head[u];i;i = G.G[i].Next) {
if (G.G[i].w) continue;
int v = G.G[i].to;
if (dis[u] == dis[v]) {
ans = (ans + dp_s[u] * dp_t[v] % MOD + dp_s[v] * dp_t[u] % MOD) % MOD;
G.G[i].w = G.G[i ^ 1].w = 1;
}
}
}
ans = (ans + dp_s[t]) % MOD;
printf("%I64d\n", ans);
}
return 0;
}

posted @ 2022-08-31 14:31  AE酱  阅读(14)  评论(0编辑  收藏  举报