题意
正权无向图$G$
数据范围
题解
容易想到的做法是d[u] + w == d[v]
是否成立即可
AC代码
#include <bits/stdc++.h>
#define maxn 505
#define inf 100000000005LL
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
vector<pll> G[maxn];
ll d[maxn][maxn], ans[maxn][maxn];
bool vis[maxn][maxn];
void bfs(ll u);
int main()
{
ll n, m, q, u, v, w;
ios::sync_with_stdio(false);
//freopen("2020dlcterm2/day06/E/1.in", "r", stdin);
//freopen("2020dlcterm2/day06/E/1.out", "w", stdout);
cin >> n >> m >> q;
for (ll i = 1; i <= n; i++) {
for (ll j = 1; j <= n; j++) {
d[i][j] = i == j ? 0 : inf;
}
}
for (ll i = 1; i <= m; i++) {
cin >> u >> v >> w;
G[u].push_back(pll(v, w));
G[v].push_back(pll(u, w));
d[u][v] = d[v][u] = w;
}
for (ll k = 1; k <= n; k++) {
for (ll i = 1; i <= n; i++){
for (ll j = 1; j <= n; j++){
d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
}
}
}
for (ll i = 1; i <= n; i++){
bfs(i);
}
for (ll i = 1; i <= q; i++)
{
cin >> u >> v;
ll outans = 0;
for (ll j = 1; j <= n; j++) {
if(d[u][j] + d[j][v] == d[u][v]) {
outans += ans[u][j];
}
}
cout << outans << '\n';
//cout << "u,v:" << u << ' ' << v << ' ' << "d:" << d[u][v] << " ans:"
//cout << "vu:" << ans[v][u] << '\n';
}
return 0;
}
ll depth[maxn];
struct pt{
ll d, id;
bool operator<(const pt& pt2) const {
return d < pt2.d;
}
};
priority_queue<pt> que;
void bfs(ll u)
{
ans[u][u] = -1;
que.push(pt{-d[u][u], u});
pt tmp;
//cout << "bfs:" << u << endl;
while(!que.empty())
{
tmp = que.top();
que.pop();
ans[u][tmp.id]++;
if(vis[u][tmp.id]) {
continue;
}
vis[u][tmp.id] = true;
//if(tmp.fencha)
//cout << tmp.id << ' ' << tmp.cnte << ' ' << tmp.fencha << endl;
//ll cntson = 0;
for (auto e : G[tmp.id])
{
if(d[u][tmp.id] + e.second == d[u][e.first]) {
que.push(pt{-d[u][e.first], e.first});
}
}
}
}