Initial commit: C++ courseware
This commit is contained in:
commit
4a4ee7c287
1789 files changed
+373489
No files matched your search
@@ -0,0 +1,29 @@
|
||||
// P1144 最短路计数。C++17;无向无权图,重边按不同边计数。
|
||||
#include <bits/stdc++.h>
|
||||
using namespace std;
|
||||
int main() {
|
||||
ios::sync_with_stdio(false); cin.tie(nullptr);
|
||||
int n, m; if (!(cin >> n >> m)) return 0;
|
||||
vector<int> head(n + 1, -1), to(2 * m), nxt(2 * m);
|
||||
int tot = 0;
|
||||
auto add = [&](int u, int v) {
|
||||
to[tot] = v; nxt[tot] = head[u]; head[u] = tot++;
|
||||
};
|
||||
for (int i = 0, u, v; i < m; ++i) {
|
||||
cin >> u >> v; add(u, v); add(v, u);
|
||||
}
|
||||
vector<int> dist(n + 1, -1), ways(n + 1, 0);
|
||||
queue<int> q; dist[1] = 0; ways[1] = 1; q.push(1);
|
||||
while (!q.empty()) {
|
||||
int u = q.front(); q.pop();
|
||||
for (int e = head[u]; e != -1; e = nxt[e]) {
|
||||
int v = to[e];
|
||||
if (dist[v] == -1) {
|
||||
dist[v] = dist[u] + 1; q.push(v);
|
||||
}
|
||||
if (dist[v] == dist[u] + 1)
|
||||
ways[v] = (ways[v] + ways[u]) % 100003;
|
||||
}
|
||||
}
|
||||
for (int i = 1; i <= n; ++i) cout << ways[i] << '\n';
|
||||
}
|
||||
Reference in new issue
Block a user