Pagini recente » Istoria paginii runda/crasfd | Cod sursa (job #1781379) | Rating Todor Aron (todoraron) | Istoria paginii runda/oo | Cod sursa (job #2029386)
#include <bits/stdc++.h>
#define pp pair<int,int>
#define x first
#define y second
using namespace std;
const int N_MAX = 50000 + 5;
const int INF = INT_MAX / 2;
int n, m;
vector<pp> vec[N_MAX];
priority_queue <pp, vector<pp >, greater<pp > > q;
int dist[N_MAX];
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
void teldrum (int source){
for(int i = 2; i<=n; ++i)
dist[i] = INF;
q.push({0, source});
while(!q.empty()){
int nod = q.top().y;
int cost = q.top().x;
q.pop();
if(cost > dist[nod])
continue;
for(auto v : vec[nod]){
if(dist[v.x] > cost + v.y){
dist[v.x] = cost + v.y;
q.push({dist[v.x], v.x});
}
}
}
}
int main()
{
fin >> n >> m;
for(int i = 1, a, b, c; i<=m; ++i){
fin >> a >> b >> c;
vec[a].push_back({b,c});
}
teldrum(1);
for(int i = 2; i<=n; ++i)
fout << dist[i] << " ";
return 0;
}