Pagini recente » Cod sursa (job #1273113) | Cod sursa (job #2184737) | Cod sursa (job #2517852) | Cod sursa (job #242919) | Cod sursa (job #2937005)
/// Preset de infoarena
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("dmin.in");
ofstream fout("dmin.out");
const int maxN = 1505, inf = 0x3f3f3f3f, mod = 104659;
int n, m, dist[maxN], ans[maxN];
bool used[maxN];
struct heapNode {
int nod, cost;
bool operator < (const heapNode &other) const
{
return cost > other.cost;
}
};
vector <heapNode> G[maxN];
priority_queue <heapNode> heap;
void dijkstra()
{
for(int i = 1; i <= n; i++)
dist[i] = inf;
dist[1] = 0;
ans[1] = 1;
heap.push({1, 0});
while(!heap.empty())
{
heapNode curr = heap.top();
heap.pop();
if(used[curr.nod])
continue;
used[curr.nod] = 1;
for(heapNode nxt : G[curr.nod])
{
if(dist[curr.nod] + nxt.cost < dist[nxt.nod])
{
dist[nxt.nod] = dist[curr.nod] + nxt.cost;
ans[nxt.nod] = 0;
heap.push({nxt.nod, dist[nxt.nod]});
}
if(dist[curr.nod] + nxt.cost == dist[nxt.nod])
ans[nxt.nod] += ans[curr.nod];
}
}
}
int main()
{
fin >> n >> m;
for(int i = 1; i <= m; i++)
{
int x, y, c;
fin >> x >> y >> c;
G[x].push_back({y, c});
}
dijkstra();
for(int i = 2; i <= n; i++)
fout << ans[i] << ' ';
return 0;
}