Pagini recente » Cod sursa (job #1057618) | Cod sursa (job #1029641) | Cod sursa (job #2889596) | Cod sursa (job #1714217) | Cod sursa (job #2574844)
#include <bits/stdc++.h>
#define NMAX 50005
using namespace std;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
vector< pair<int, int> > G[NMAX];
vector< int > cost(NMAX, -1);
void dijkstra(){
priority_queue< pair<int, int>, vector< pair<int, int> >, greater< pair<int, int> > >PQ;
cost[1]=0;
PQ.push(make_pair(0, 1));
while(!PQ.empty()){
int nd = PQ.top().second;
int ct = PQ.top().first;
PQ.pop();
if(cost[nd]!=ct)continue;
for(auto it: G[nd]){
if(cost[it.first]==-1 || cost[it.first]> ct+it.second){
cost[it.first]=ct + it.second;
PQ.push(make_pair(ct+it.second, it.first));
}
}
}
}
int main()
{
int n,m;
f>>n>>m;
for(int i=1,x,y,c;i<=m;i++){
f>>x>>y>>c;
G[x].push_back(make_pair(y,c));
G[y].push_back(make_pair(x,c));
}
dijkstra();
for(int i=2;i<=n;i++)
g<<cost[i]<<' ';
return 0;
}