Pagini recente » Cod sursa (job #258691) | Cod sursa (job #3197209) | Cod sursa (job #1150391) | Cod sursa (job #495403) | Cod sursa (job #2264903)
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
#define lim 250000
#define inf 20000
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];
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();
for (int i=0;i<muc[nod].size();i++)
{
if (dist[nod]-muc[nod][i].first<dist[muc[nod][i].second])
{
dist[muc[nod][i].second]=dist[nod]-muc[nod][i].first;
pq.push({(-1)*muc[nod][i].first,muc[nod][i].second});
}
}
}
for (int i=2;i<=n;i++)
if (dist[i]==inf) g<<0<<" ";
else g<<dist[i]<<" ";
f.close();
g.close();
return 0;
}