Pagini recente » Cod sursa (job #374775) | Cod sursa (job #2694969) | Cod sursa (job #2902037) | Cod sursa (job #2496101) | Cod sursa (job #1933698)
#include <fstream>
#include <iostream>
#include <queue>
#include <vector>
#define INF 99999999
using namespace std;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
int n,m,x,y,i,cost,d[50001],nodac,costac;
bool viz[50001];
vector<pair <int,int > > G[50001];
struct compare{
bool operator()(pair<int,int> x, pair<int, int> y){
return x.second>y.second;
}
};
priority_queue<pair <int, int>, vector<pair <int, int> >, compare> heap;
int main()
{
f>>n>>m;
for(i=1;i<=n;i++)
d[i]=INF;
heap.push(make_pair(1,0));
d[1]=0;
for(i=1;i<=m;i++)
{
f>>x>>y>>cost;
G[x].push_back(make_pair(y,cost));
}
while(!heap.empty()){
pair<int,int> nod=heap.top();
heap.pop();
if(viz[nod.first]==1)
continue;
viz[nod.first]=1;
for(i=0;i<G[nod.first].size();i++){
nodac=G[nod.first][i].first;
costac=G[nod.first][i].second;
if(viz[nodac]==0)
if(d[nodac]>d[nod.first]+costac){
d[nodac]=d[nod.first]+costac;
heap.push(make_pair(nodac,d[nodac]));
}
}
}
for(i=2;i<=n;i++)
if(d[i]!=INF)
g<<d[i]<<" ";
else
g<<0<<" ";
return 0;
}