Pagini recente » Cod sursa (job #1308152) | Cod sursa (job #2596534) | Cod sursa (job #3263256) | Cod sursa (job #3141671) | Cod sursa (job #2286273)
#include <bits/stdc++.h>
#define INF 0x3f3f3f3
#define NMAX 50500
using namespace std;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
int n,m;
vector< vector< pair< int, int> > > G;
void citire(){
f>>n>>m;
G.resize(n+2);
for(int i=1,x,y,c;i<=m;i++){
f>>x>>y>>c;
G[x].push_back(make_pair(y,c));
}
}
void dijkstra(){
vector <int> cost(n+2, INF);
priority_queue< pair<int,int>, vector< pair<int,int> >, greater< pair<int,int> > >Q;
Q.push(make_pair(0,1));
cost[1]=0;
while(!Q.empty()){
int node=Q.top().second;
int ct=Q.top().first;
Q.pop();
if(ct != cost[node]) continue;
for(auto it:G[node]){
if(cost[it.first]>ct+it.second){
cost[it.first]=ct+it.second;
Q.push(make_pair(cost[it.first],it.first));
}
}
}
for(int i=2;i<=n;i++){
if(cost[i]==INF)g<<"0 ";
else
g<<cost[i]<<" ";
}
}
int main()
{
citire();
dijkstra();
return 0;
}