Pagini recente » Cod sursa (job #1766791) | Cod sursa (job #1949542) | Cod sursa (job #2115032) | Cod sursa (job #1366481) | Cod sursa (job #2945847)
#include <bits/stdc++.h>
using namespace std;
ifstream f("dmin.in");
ofstream g("dmin.out");
int n, m, x, y, cs, d[1501], viz[1501], niv[1501], mnl[1501], nr[1501];
vector<pair<int, int>> v[1501];
void citire()
{
f>>n>>m;
for(int i=1; i<=m; i++)
{
f>>x>>y>>cs;
v[x].push_back({y, cs});
v[y].push_back({x, cs});
}
}
void dijkstra(int st)
{
priority_queue<pair<int, int>> q;
for(int i=1; i<=n; i++)d[i]=INT_MAX;
q.push({0, st});
d[st]=1;
while(!q.empty())
{
int x=q.top().second;
q.pop();
if(viz[x])continue;
viz[x]=1;
for(auto i:v[x])
{
int vcn=i.first, cst=i.second;
if(d[x]*cst==d[vcn])mnl[vcn]+=mnl[x];
if(d[x]*cst<d[vcn])
{
mnl[vcn]=mnl[x];
d[vcn]=d[x]*cst;
if(!viz[vcn])q.push({-d[vcn], vcn});
}
}
}
}
void rez()
{
mnl[1]=1;
dijkstra(1);
for(int i=2; i<=n; i++)g<<mnl[i]<<" ";
}
int main()
{
citire();
rez();
return 0;
}