Pagini recente » Cod sursa (job #1652666) | Cod sursa (job #925969) | Cod sursa (job #1574939) | Cod sursa (job #2194968) | Cod sursa (job #2283864)
#include <fstream>
#include <vector>
std::ifstream cin("dijkstra.in");
std::ofstream cout("dijkstra.out");
#define it std::vector<std::pair<int,int>>::iterator
const int INF = 1<<30;
const int maxn = 50050;
std::vector<std::pair<int,int>> g[maxn];
int n,m,k;
int h[maxn],poz[maxn],d[maxn],viz[maxn];
void swap(int a,int b){int aux;aux=h[a];h[a]=h[b];h[b]=aux;}
void dijkstra_wHeap();
void downheap(int);
void upheap(int);
int main()
{
int x,y,z;
cin>>n>>m;
for(;--m;){
cin>>x>>y>>z;
g[x].push_back(std::make_pair(y,z));
}
dijkstra_wHeap();
for(int i=2;i<=n;i++){
x=((d[i]==INF)?0:d[i]);
cout<<x<<' ';
}
cout<<'\n';
return 0;
}
void dijkstra_wHeap(){
for(int i=2;i<=n;i++)
d[i]=INF, poz[i]=-1;
h[++k]=1;
poz[1]=1;
while(k){
int mini=h[1];
viz[h[1]]=1;
swap(k,1);
poz[h[1]]=1;
--k;
downheap(1);
for(it xd=g[mini].begin();xd!=g[mini].end();xd++){
if(d[xd->first]>d[mini]+xd->second&&!viz[xd->first]){ //xd pointer ->
d[xd->first]=d[mini]+xd->second; //nu se foloseste .
if(poz[xd->first]!=-1)
upheap(poz[xd->first]);
else{
h[++k]=xd->first;
poz[h[k]]=k;
upheap(k);
}
}
}
}
}
void upheap(int x){
int tata=x;
while(x>1){
tata=x>>1;
if(d[h[tata]]>d[h[x]]){
poz[h[tata]]=x;
poz[h[x]]=tata;
swap(x,tata);
x=tata;
}else
x=1;
}
}
void downheap(int x){
int f;
while(x<=k){
f=x;
if((x<<1)<=k){
f=x<<1;
if(f+1<=k)
if(d[h[f]]>d[h[f+1]])
f++;
}
else
return;
if(d[h[f]]<d[h[x]]){
poz[h[f]]=x;
poz[h[x]]=f;
swap(f,x);
x=f;
}
else
return;
}
}