Pagini recente » Cod sursa (job #998539) | Cod sursa (job #967992) | Cod sursa (job #145526) | Cod sursa (job #2085173) | Cod sursa (job #1564912)
#include <cstdio>
#include <vector>
#include <queue>
#define MAXN 50010
#define INF 1<<30
using namespace std;
vector<int> d;
queue<int> Q;
struct nod{int nd; int cost; nod *next;};
nod *L[MAXN];
int n, m, uz[MAXN], S;
void citire()
{
nod *p;
int i, x, y, cost;
scanf("%d%d", &n, &m);
for(i=1;i<=m;++i)
{
scanf("%d%d%d", &x, &y, &cost);
p=new nod;
p->nd=y;
p->cost=cost;
p->next=L[x];
L[x]=p;
}
S=1;
}
void dijkstra()
{
int k;
d.assign(n+1, INF);
d[S]=0;
uz[S]=1;
Q.push(S);
while(!Q.empty())
{
k=Q.front();
Q.pop();
uz[k]=0;
nod *p=L[k];
while(p)
{
if(d[p->nd]>d[k]+p->cost)
{
d[p->nd]=d[k]+p->cost;
if(!uz[p->nd])
{
Q.push(p->nd);
uz[p->nd]=1;
}
}
p=p->next;
}
}
}
void afisare()
{
int i;
for(i=2;i<=n;++i)
if(d[i]!=INF)
printf("%d ", d[i]);
else
printf("0 ");
printf("\n");
}
void rezolva_problema()
{
citire();
dijkstra();
afisare();
}
int main()
{
freopen("dijkstra.in", "r", stdin);
freopen("dijkstra.out", "w", stdout);
rezolva_problema();
return 0;
}