Pagini recente » Cod sursa (job #2922402) | Cod sursa (job #495731) | Cod sursa (job #2668908) | Cod sursa (job #2695626) | Cod sursa (job #689832)
Cod sursa(job #689832)
#include <fstream>
using namespace std;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
struct nod
{
int x,cost;
nod*urm;
}*A[250005],*p;
int n,m,i,a,b,c,culoare[250005],cc[250005],coada[250005],in=1,sf=0;
int main()
{
f>>n>>m;
for(i=1;i<=n;i++)
{
f>>a>>b>>c;
p=new nod;
p->x=a;
p->cost=c;
p->urm=A[b];
A[b]=p;
p=new nod;
p->x=b;
p->cost=c;
p->urm=A[a];
A[a]=p;
}
cc[1]=0;
culoare[1]=2;
coada[++sf]=1;
while(in<=sf)
{
p=A[in];
while(p)
{
if((culoare[p->x]==2 && p->x!=1) || !culoare[p->x]) culoare[p->x]++,cc[p->x]=cc[in]+p->cost,coada[++sf]=p->x;
else if(culoare[p->x] == 1 && cc[p->x]>cc[in]+p->cost) cc[p->x]=cc[in]+p->cost;
p=p->urm;
}
in++;
}
for(i=2;i<=n;i++)g<<cc[i]<<' ';
return 0;
}