Pagini recente » Cod sursa (job #2967398) | Cod sursa (job #2631863) | Cod sursa (job #1011462) | Cod sursa (job #2292906) | Cod sursa (job #2264921)
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
#define lim 250000
#define inf 20005
using namespace std;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
priority_queue <pair <int,int> > pq;
vector <pair <int,int> > muc[lim];
int n,m,a,b,c,dist[lim];
bool viz[lim];
int main()
{
f>>n>>m;
for (int i=1;i<=m;i++)
{
f>>a>>b>>c;
muc[a].push_back({(-1)*c,b});
}
for (int i=2;i<=n;i++) dist[i]=inf;
pq.push({0,1});
while (!pq.empty())
{
int nod=pq.top().second;
pq.pop();
if (viz[nod]) continue;
viz[nod]=1;
for (auto vec:muc[nod])
{
if (dist[nod]-vec.first<dist[vec.second])
{
dist[vec.second]=dist[nod]-vec.first;
pq.push({(-1)*vec.first,vec.second});
}
}
}
for (int i=2;i<=n;i++)
if (dist[i]==inf) g<<0<<" ";
else g<<dist[i]<<" ";
f.close();
g.close();
return 0;
}