Pagini recente » Cod sursa (job #2434293) | Cod sursa (job #814077) | Cod sursa (job #875724) | Cod sursa (job #1894004) | Cod sursa (job #2829404)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
const int NMAX=50005;
const int inf=2e9;
int N, M, d[NMAX];
vector<pair<int,int>> g[NMAX];
struct element{
int nod, cost;
};
struct compare{
bool operator()(element &A, element &B){
return A.cost>B.cost;
}
};
priority_queue<element,vector<element>,compare> q;
void Dijkstra(int sursa)
{
for(int i=1;i<=N;i++)
d[i]=inf;
d[sursa]=0;
q.push({sursa,0});
element x;
while(!q.empty()){
x=q.top();
q.pop();
if(x.cost!=d[x.nod]) /// am gasit un nod duplicat neactualizat
continue;
for(auto next: g[x.nod]){
if(d[x.nod]+next.second<d[next.first]){
d[next.first]=d[x.nod]+next.second;
q.push({next.first,d[next.first]});
}
}
}
}
int main()
{
fin>>N>>M;
int x, y, z;
for(int i=1;i<=M;i++){
fin>>x>>y>>z;
g[x].push_back({y,z});
}
Dijkstra(1);
for(int i=2;i<=N;i++)
if(d[i]==inf)
fout<<0<<' ';
else
fout<<d[i]<<' ';
fin.close();
fout.close();
return 0;
}