Pagini recente » Cod sursa (job #1481941) | Cod sursa (job #301547) | Cod sursa (job #2233422) | Cod sursa (job #2020774) | Cod sursa (job #1650082)
#include <iostream>
#include <stdio.h>
#include <queue>
#include <vector>
#define inf 0x7fffffff
using namespace std;
int n,m,d[50050];
struct muchie
{
int x,c;
};
struct cmp
{
bool operator() (int a, int b)
{
return d[a]>d[b];
}
};
vector <muchie> g[50050];
priority_queue <int, vector<int>, cmp> q;
void citire()
{
int a,b,c;
scanf("%d %d",&n,&m);
for(int i=0;i<m;i++)
{
scanf("%d %d %d",&a,&b,&c);
muchie aux;
aux.x=b;
aux.c=c;
g[a].push_back(aux);
}
}
void djk()
{
q.push(1);
for(int i=2;i<=n;i++)
d[i]=inf;
while(!q.empty())
{
int t=q.top();
q.pop();
for(int i=0;i<g[t].size();i++)
if(d[g[t][i].x]>d[t]+g[t][i].c)
{
d[g[t][i].x]=d[t]+g[t][i].c;
q.push(g[t][i].x);
}
}
}
void afisare()
{
for(int i=2;i<=n;i++)
if(d[i]!=inf)
printf("%d ",d[i]);
else
printf("0 ");
}
int main()
{
freopen("dijkstra.in","r",stdin);
freopen("dijkstra.out","w",stdout);
citire();
djk();
afisare();
return 0;
}