Pagini recente » Cod sursa (job #2802506) | Cod sursa (job #1402343) | Istoria paginii runda/threedays_ultimate_challenge/clasament | Cod sursa (job #1589833) | Cod sursa (job #2420640)
#include <bits/stdc++.h>
using namespace std;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
const int NMAX = 50005;
const int inf = 1e9;
priority_queue< pair<int,int> > q;
vector <pair <int,int > > v[NMAX];
int n,m,dist[NMAX];
bool viz[NMAX];
int main(){
int i,a,b,c;
f >> n >> m;
for(i = 1 ; i <= m ; i++){
f >> a >> b >> c;
v[a].push_back({b,c});
}
for(i = 2 ; i <= n ; i++)
dist[i] = inf;
q.push({0,1});
while(!q.empty()){
int nod = q.top().second;
q.pop();
if(viz[nod])
continue;
for (auto it: v[nod]){
if (it.second + dist[nod] < dist[it.first]){
dist[it.first] = it.second + dist[nod];
q.push({-dist[it.first],it.first});
}
}
}
for(i = 2 ; i <= n ; i++)
if(dist[i] == inf)
g << 0 << " ";
else
g << dist[i] << " ";
return 0;
}