Pagini recente » Cod sursa (job #397103) | Cod sursa (job #2058622) | Cod sursa (job #3256665) | Cod sursa (job #2564122) | Cod sursa (job #2699619)
#include <fstream>
#include <queue>
#include <vector>
using namespace std;
ifstream in ("dijkstra.in");
ofstream out("dijkstra.out");
const int E=1000000000;
const int maxn=50001;
int dist[maxn],n,m;
bool viz[maxn];
vector <pair<int,int> > v[maxn];
void dijkstra()
{
int i;
dist[1]=0;
for (i=2;i<=n;i++){
dist[i]=E;
}
priority_queue <pair<int,int> >q;
q.push({0,1});
while(q.size()){
int node=q.top().second;
if (viz[node]) continue;
for (i=0;i<v[node].size();i++){
int d=v[node][i].second;
int x=v[node][i].first;
if (dist[node]+d<dist[x]){
dist[x]=dist[node]+d;
q.push({-dist[x],x});
}
}
q.pop();
viz[node]=1;
}
return;
}
int main()
{
int i,x,y,d;
in>>n>>m;
for (i=0;i<m;i++){
in>>x>>y>>d;
v[x].push_back({y,d});
}
dijkstra();
for(i=2;i<=n;i++){
if (dist[i]==E) out<<0<<" ";
else out<<dist[i]<<" ";
}
return 0;
}