# Cod sursa(job #2344013)

Utilizator Data 14 februarie 2019 17:37:56 Drumuri minime 25 cpp-64 done Arhiva de probleme 1.73 kb
``````#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <cmath>
#define Nmax 1505
#define INF 0x3f3f3f3f

using namespace std;

ifstream f("dmin.in");
ofstream g("dmin.out");

double EPS=1e-8;
double d[Nmax];
int cnt[Nmax];
bool in[Nmax];
struct cmp{
bool operator()(int x, int y)
{
return d[x]-d[y]>0;
}
};

int n, m;
vector <pair <int, int> > v[Nmax];
priority_queue <int, vector <int>, cmp> Q;

void dijkstra(int start)
{
for (int i = 1; i <= n; i++)
{
d[i]=INF;
}
cnt[1]=1;
d[start]=0;
in[start]=1;
Q.push(start);
while(!Q.empty())
{
int x=Q.top();
in[x]=0;
Q.pop();
for (int i = 0; i < v[x].size(); i++)
{
int y=v[x][i].first;
double c=v[x][i].second;
c=log2(c);
if(d[y]-d[x]-c>EPS)
{
cnt[y]=cnt[x];
d[y]=d[x]+c;
if(in[y] == 0)
{
Q.push(y);
in[y]=1;
}
}
else
{
if(abs(d[y]-d[x]-c)<EPS)
{
cnt[y]+=cnt[x];
if(in[y] == 0)
{
Q.push(y);
in[y]=1;
}
}
}
}
}

for (int i = 2; i <= n; i++) g << cnt[i] << " " ;

}

int main()
{
f >> n >> m;
for (int i = 1; i <= m; i++)
{
int x, y, c;
f >> x >> y >> c;
v[x].push_back({y, c});
v[y].push_back({x, c});
}

dijkstra(1);

return 0;
}
``````