最短路计数
题目描述
给出一个
输入格式
第一行包含
接下来
输出格式
共
样例 #1
样例输入 #1
5 7
1 2
1 3
2 4
3 4
2 3
4 5
4 5样例输出 #1
1
1
1
2
4提示
对于
对于
对于
题解
c++
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
const int mod = 100003;
void solve()
{
int n, m;
cin >> n >> m;
vector<pair<int, int>> adj[n + 1];
for (int i = 1; i <= m; i++)
{
int u, v;
cin >> u >> v;
adj[u].push_back({v, 1});
adj[v].push_back({u, 1});
}
vector<int> dis(n + 1, 1e9), ans(n + 1);
vector<bool> vis(n + 1);
priority_queue<pair<int, int>> q;
dis[1] = 0;
ans[1] = 1;
q.push({0, 1});
while (!q.empty()) // Dijkstra
{
auto [x, y] = q.top();
q.pop();
if (vis[y] == true)
{
continue;
}
vis[y] = true;
for (auto [v, w] : adj[y])
{
if (dis[v] > dis[y] + w)
{
dis[v] = dis[y] + w;
ans[v] = ans[y];
q.push({-dis[v], v});
}
else if (dis[v] == dis[y] + 1)
{
ans[v] += ans[y];
ans[v] %= mod;
}
}
}
for (int i = 1; i <= n; i++)
{
cout << ans[i] << '\n';
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int _ = 1;
// cin >> _;
while (_--)
{
solve();
}
return 0;
}