Pagini recente » Clasament preojii | Cod sursa (job #289037) | Cod sursa (job #2260873) | Cod sursa (job #3135325) | Cod sursa (job #2181663)
#include <bits/stdc++.h>
#define nmax 50005
using namespace std;
ifstream in("dijkstra.in");
ofstream out("dijkstra.out");
vector < pair < int , int >> v[nmax];
bool viz[nmax];
int dist[nmax],n,m;
void read()
{
in >> n >> m;
int x,y,c;
for(int i=1; i<=m; i++)
{
in >> x >> y >> c;
v[x].push_back({y,c});
}
for(int i=2; i<=n; i++) dist[i] = 1<<30;
}
int djk()
{
priority_queue < pair < int, int >> q;
q.push({0,1});
while( !q.empty())
{
int head = q.top().second;
q.pop();
if( viz[head] ) continue;
viz[head] = 1;
for(int i=0; i<v[head].size(); i++)
{
int j = v[head][i].first;
int cost = v[head][i].second;
if( dist[j] > dist[head] + cost )
{
dist[j] = dist[head] + cost;
q.push({-dist[j],j});
}
}
}
}
int main()
{
ios::sync_with_stdio(0);
read();
djk();
for(int i=2; i<=n; i++)
( dist[i] == 1 << 30 ) ? out << 0 << ' ' : out << dist[i] << ' ';
return 0;
}