Pagini recente » Cod sursa (job #684360) | Cod sursa (job #1602737) | Cod sursa (job #2397174) | Cod sursa (job #861560) | Cod sursa (job #2039778)
#include <fstream>
#include <vector>
#include <queue>
#include <algorithm>
#define MAXN 50005
#define INF 2000000000
using namespace std;
ifstream fi("dijkstra.in");
ofstream fo("dijkstra.out");
int n,m;
vector < pair<int,int> > G[MAXN];
int dist[MAXN];
bool viz[MAXN];
bool cmp(int a,int b)
{
return dist[a]<dist[b];
}
int main()
{
fi>>n>>m;
for (int i=1; i<=m; i++)
{
int u,v,c;
fi>>u>>v>>c;
G[u].push_back({v,c});
}
for (int i=2; i<=n; i++)
dist[i]=INF;
priority_queue < pair<int,int> > V;
for (int i=1; i<=n; i++)
V.push(make_pair(INF-dist[i],i));
while (!V.empty())
{
int nod=V.top().second;
V.pop();
if (viz[nod])
continue;
viz[nod]=1;
///expandam pe nod
for (auto copil: G[nod])
{
if (dist[nod]+copil.second<dist[copil.first])
{
dist[copil.first]=dist[nod]+copil.second;
V.push(make_pair(INF-dist[copil.first],copil.first));
}
}
}
for (int i=2; i<=n; i++)
if (dist[i]==INF)
fo<<"0 ";
else
fo<<dist[i]<<" ";
return 0;
}