Pagini recente » Borderou de evaluare (job #2016265) | Cod sursa (job #3140059) | Cod sursa (job #2383962) | Cod sursa (job #905704) | Cod sursa (job #556854)
Cod sursa(job #556854)
#include<stdio.h>
#include<algorithm>
#define nmax 250001
using namespace std ;
int i,j,k,m,n,dr[50001],v[nmax],a[nmax],minim,t=1,poz;
struct p
{
int x,y,c;
};
p vect[nmax];
struct cmp
{
bool operator () (const p &a,const p &b) const
{
return a.x<b.x||a.x==b.x&&a.y<b.y||a.x==b.x&&a.y==b.y&&a.c<b.c;
}
};
int main()
{
freopen("dijkstra.in","r",stdin);
freopen("dijkstra.out","w",stdout);
scanf("%d%d\n",&n,&m);
for(i=1;i<=m;i++)
scanf("%d%d%d\n",&vect[i].x,&vect[i].y,&vect[i].c);
for(i=1;i<=n;i++)
{
a[i]=1;
dr[i]=nmax;
}
sort(vect+1,vect+m+1,cmp());
/*for(i=1;i<=n;i++)
printf("%d %d %d\n",vect[i].x,vect[i].y,vect[i].c);*/
k=1;
v[k]=1;
a[v[k]]=0;
dr[v[k]]=0;
while(t)
{
t=0;
minim=nmax;
for(i=1;i<=k;i++)
{
for(j=1;j<=m&&vect[j].x<=v[i];j++)
{
if(vect[j].x==v[i]&&a[vect[j].y])
if(vect[j].c<minim)
{
minim=vect[j].c;
poz=j;
t=1;
}
}
}
if(t)
{
if(a[vect[poz].y])
{
k++;
v[k]=vect[poz].y;
dr[vect[poz].y]=dr[vect[poz].x]+vect[poz].c;
a[vect[poz].y]=0;
}
else
if(dr[vect[poz].y]>dr[vect[poz].x]+vect[poz].c)
dr[vect[poz].y]=dr[vect[poz].x]+vect[poz].c;
}
}
for(i=2;i<=n;i++)
printf("%d ",dr[i]);
return 0;
}