Pagini recente » Cod sursa (job #2820795) | Cod sursa (job #2809705) | Cod sursa (job #2426923) | Cod sursa (job #1267352) | Cod sursa (job #2906896)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream cin("dijkstra.in");
ofstream cout("dijkstra.out");
int d[200005],pred[200005];
vector<pair<int,int>> v[200005],ans;
priority_queue<pair<int,int>> q;
bool viz[200005];
int main()
{
int n,m;
cin>>n>>m;
for(int i=2;i<=n;i++)
d[i] = 1<<30; // initializam distantele cu un numar foarte mare
for(int i = 1; i <= m; i++)
{
int x, y, cost;
cin >> x >> y >> cost; // citim pe rand muchiile si costul lor
v[x].push_back({y,cost}); // matricea de adiacenta
}
q.push({0,1}); // pornim de la nodul 1
while(!q.empty())
{
int x = q.top().second;
q.pop();
if(viz[x])
continue;
viz[x] = true;
for(auto i:v[x])
{
int y = i.first;
int cost = i.second;
if(!viz[y] && d[y] > cost + d[x])
d[y] = cost + d[x], q.push({-cost - d[x], y});
}
}
for(int i = 2; i <= n; i++)
{
if(d[i] == 1<<30)
d[i]=0;
cout<<d[i]<<" ";
}
return 0;
}