|
|
|
@ -93,26 +93,26 @@ void add(int a, int b) {
|
|
|
|
|
e[idx] = b, ne[idx] = h[a], h[a] = idx++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int cnt[N]; //从顶点1开始,到其他每个点的最短路有几条
|
|
|
|
|
int dist[N]; //最短距离
|
|
|
|
|
int cnt[N]; // 从顶点1开始,到其他每个点的最短路有几条
|
|
|
|
|
int dis[N]; // 最短距离
|
|
|
|
|
int n, m;
|
|
|
|
|
void bfs() {
|
|
|
|
|
memset(dist, 0x3f, sizeof dist);
|
|
|
|
|
memset(dis, 0x3f, sizeof dis);
|
|
|
|
|
queue<int> q;
|
|
|
|
|
q.push(1);
|
|
|
|
|
cnt[1] = 1; //从顶点1开始,到顶点1的最短路有1条
|
|
|
|
|
dist[1] = 0; //距离为0
|
|
|
|
|
cnt[1] = 1; // 从顶点1开始,到顶点1的最短路有1条
|
|
|
|
|
dis[1] = 0; // 距离为0
|
|
|
|
|
|
|
|
|
|
while (q.size()) {
|
|
|
|
|
int u = q.front();
|
|
|
|
|
q.pop();
|
|
|
|
|
for (int i = h[u]; ~i; i = ne[i]) {
|
|
|
|
|
int j = e[i];
|
|
|
|
|
if (dist[j] > dist[u] + 1) {
|
|
|
|
|
dist[j] = dist[u] + 1;
|
|
|
|
|
if (dis[j] > dis[u] + 1) {
|
|
|
|
|
dis[j] = dis[u] + 1;
|
|
|
|
|
q.push(j);
|
|
|
|
|
cnt[j] = cnt[u];
|
|
|
|
|
} else if (dist[j] == dist[u] + 1)
|
|
|
|
|
} else if (dis[j] == dis[u] + 1)
|
|
|
|
|
cnt[j] = (cnt[j] + cnt[u]) % MOD;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -141,7 +141,7 @@ const int MOD = 100003;
|
|
|
|
|
|
|
|
|
|
int n, m;
|
|
|
|
|
int cnt[N];
|
|
|
|
|
int dist[N];
|
|
|
|
|
int dis[N];
|
|
|
|
|
bool st[N];
|
|
|
|
|
int h[N], e[M], ne[M], idx;
|
|
|
|
|
void add(int a, int b) {
|
|
|
|
@ -149,30 +149,29 @@ void add(int a, int b) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void dijkstra() {
|
|
|
|
|
memset(dist, 0x3f, sizeof dist);
|
|
|
|
|
dist[1] = 0;
|
|
|
|
|
// 出发点到自己的最短路径只能有1条
|
|
|
|
|
cnt[1] = 1;
|
|
|
|
|
memset(dis, 0x3f, sizeof dis);
|
|
|
|
|
dis[1] = 0;
|
|
|
|
|
cnt[1] = 1; // 出发点到自己的最短路径有1条,长度是0
|
|
|
|
|
|
|
|
|
|
// 小顶堆q
|
|
|
|
|
priority_queue<PII, vector<PII>, greater<PII>> pq;
|
|
|
|
|
pq.push({0, 1});
|
|
|
|
|
priority_queue<PII, vector<PII>, greater<PII>> q;
|
|
|
|
|
q.push({0, 1});
|
|
|
|
|
|
|
|
|
|
while (pq.size()) {
|
|
|
|
|
auto t = pq.top();
|
|
|
|
|
pq.pop();
|
|
|
|
|
int u = t.second, d = t.first;
|
|
|
|
|
while (q.size()) {
|
|
|
|
|
auto t = q.top();
|
|
|
|
|
q.pop();
|
|
|
|
|
int u = t.second;
|
|
|
|
|
|
|
|
|
|
if (st[u]) continue;
|
|
|
|
|
st[u] = true;
|
|
|
|
|
|
|
|
|
|
for (int i = h[u]; ~i; i = ne[i]) {
|
|
|
|
|
int j = e[i];
|
|
|
|
|
if (dist[j] > d + 1) {
|
|
|
|
|
dist[j] = d + 1;
|
|
|
|
|
if (dis[j] > dis[u] + 1) {
|
|
|
|
|
dis[j] = dis[u] + 1;
|
|
|
|
|
cnt[j] = cnt[u];
|
|
|
|
|
pq.push({dist[j], j});
|
|
|
|
|
} else if (dist[j] == d + 1)
|
|
|
|
|
q.push({dis[j], j});
|
|
|
|
|
} else if (dis[j] == dis[u] + 1)
|
|
|
|
|
cnt[j] = (cnt[j] + cnt[u]) % MOD;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|