Pagini recente » Cod sursa (job #347185) | Cod sursa (job #1129137) | Cod sursa (job #2617527) | Cod sursa (job #572727) | Cod sursa (job #153210)
Cod sursa(job #153210)
#include<cstdio>
#include<vector>
using namespace std;
int n,m,d[50001],h[50001],poz[50001],x,y,c,i,vf;
const int lim=250000*1000+1;
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=i<<1;
if(t<=m){
st=h[t];
if(t+1<=m) dr=h[t+1];
else dr=st;
if(d[dr]<d[st]) t=t+1;
if(d[h[i]]>d[h[t]]){
change(i,t);
heapdown(t);}
}
}
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();
if(d[vf]==lim)break;
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++)
if(d[i]<lim)printf("%d ",d[i]);
else printf("0 ");
fclose(stdout);
}