Pagini recente » Cod sursa (job #1329997) | Cod sursa (job #491612) | Cod sursa (job #3031690) | Cod sursa (job #2307191) | Cod sursa (job #153180)
Cod sursa(job #153180)
#include<cstdio>
#include<vector>
using namespace std;
int n,m,d[50001],h[50001],poz[50001],x,y,c,i,j,vf;
const int lim=2000000000;
vector<pair<int,int> > a[50001];
inline void change(int i,int j)
{
int a;
a=h[i];h[i]=h[j];h[j]=a;
poz[h[i]]=i;poz[h[j]]=j;
}
void heapup(int i)
{
if(i<=1) return;
int t=i>>1;
if(d[h[i]]<d[h[t]]){
change(i,t);
heapup(t);}
}
void heapdown(int i)
{
int st,dr,t;
if(2*i<=m){
st=h[2*i];
if(2*i+1<=m) dr=h[2*i+1];
else dr=st;
if(d[h[i]]>d[st]){
change(i,2*i);
heapdown(2*i);}
else
if(d[h[i]]>d[dr]){
change(i,2*i+1);
heapdown(2*i+1);}
}
}
inline void build()
{
for(i=1;i<=n;i++){
h[i]=i;
d[i]=lim;
poz[i]=i;}
d[1]=0;
}
inline int scoate()
{
change(1,m);
m--;
heapdown(1);
return h[m+1];
}
inline void dijkstra()
{
m=n;
build();
while(m>0){
vf=scoate();
x=a[vf].size();
for(i=0;i<x;i++){
y=a[vf][i].first;
c=a[vf][i].second;
if(d[y]>d[vf]+c){
d[y]=d[vf]+c;
heapup(poz[y]);}
}
}
}
int main()
{
freopen("dijkstra.in","r",stdin);
freopen("dijkstra.out","w",stdout);
scanf("%d %d",&n,&m);
for(i=1;i<=n;i++){
scanf("%d %d %d",&x,&y,&c);
a[x].push_back(make_pair(y,c));
a[y].push_back(make_pair(x,c));}
dijkstra();
for(i=2;i<=n;i++)
printf("%d ",d[i]);
fclose(stdout);
}