Pagini recente » Cod sursa (job #2823497) | Cod sursa (job #2553713) | Cod sursa (job #1569017) | Cod sursa (job #820409) | Cod sursa (job #3238183)
#include <bits/stdc++.h>
using namespace std;
const int MAX = 50005;
vector<pair<int,int>> graph[MAX];
vector<int> ans, dist;
bitset<MAX> visited;
void dijkstra(int start)
{
priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> q;
q.push({0, start});
dist[start] = 0;
ans[start] = 1;
while (!q.empty())
{
pair<int,int> node = q.top();
q.pop();
if (!visited[node.second])
{
visited[node.second] = 1;
for (pair<int,int> next : graph[node.second])
{
if (dist[next.first] > dist[node.second] + next.second)
{
dist[next.first] = dist[node.second] + next.second;
ans[next.first] = ans[node.second];
q.push({dist[next.first], next.first});
}
else if (dist[next.first] == dist[node.second] + next.second)
{
ans[next.first] += ans[node.second];
}
}
}
}
}
int main()
{
ifstream cin("dmin.in");
ofstream cout("dmin.out");
int n, m;
cin >> n >> m;
ans.assign(n+1, 0);
dist.assign(n+1, INT_MAX);
for (int i = 1; i <= m; ++i)
{
int x, y, cost;
cin >> x >> y >> cost;
graph[x].push_back({y, cost});
graph[y].push_back({x, cost});
}
dijkstra(1);
for (int i = 1; i <= n; ++i)
cout << ans[i] << " ";
return 0;
}