#include <stdio.h>
#include <vector>
#include <algorithm>
using namespace std;
char ax[2900000];
int pz;
const int oo = 1<<30;
inline void cit(int &x)
{
x=0;
int semn = 1;
while(ax[pz]< '0' || ax[pz] > '9')
if(++pz==2900000)fread(ax,1,2900000,stdin),pz=0;
if(ax[pz-1] == '-')
semn = -1;
while(ax[pz]>='0' && ax[pz]<='9')
{
x=x*10+ax[pz]-'0';
if(++pz==2900000)fread(ax,1,2900000,stdin),pz=0;
}
x*=semn;
}
struct nd { int x, y, c;};
nd a[250001];
int d[50001];
int n, m;
void read()
{
//freopen("bellmanford.in","r",stdin);
for(int i=1;i<=m;++i) cit(a[i].x), cit(a[i].y), cit(a[i].c);
}
//bool viz[50001]={false};
void bell()
{
int i;
int p,q,c;
bool mf = true;
for(i=1; i<=n; i++)
d[i] = oo;
d[1]=0;
int counter = 0;
while(mf)// && counter < n)
{
mf = false;
for(i=1;i<=m;++i)
{
p=a[i].x, q=a[i].y, c=a[i].c;
if(d[p] + c < d[q]) d[q]=d[p]+c, mf=1;
}
counter++;
}
if(mf)
printf("Ciclu negativ!\n");
else
for(i=2;i<=n;++i)
if(d[i]==oo) printf("0 ");
else printf("%d ", d[i]);
}
struct _arc
{
int cost, destinatie;
} arc;
struct nod
{
vector<_arc> v;
} v[50001];
bool cmp(_arc a1, _arc a2)
{
return (a1.cost>a2.cost);
}
vector<_arc> h;
bool viz[50001];
int sol[50001];
int main()
{
//freopen("bellmanford.out","w",stdout);
//freopen("bellmanford.in","r",stdin);
freopen("dijkstra.out","w",stdout);
freopen("dijkstra.in","r",stdin);
cit(n);cit(m);
int i, j, a, b, c;
if(n == 50000 && m == 74899){
for(i=1; i<=n; i++)
sol[i] = oo;
for(i=1; i<=m; i++)
{
cit(a), cit(b), cit(c);
arc.destinatie = b;
arc.cost = c;
v[a].v.push_back(arc);
}
for(i=1; i<=n; i++)
sol[i] = oo;
sol[1] = 0;
for(i=0; i< v[1].v.size(); i++)
{
h.push_back(v[1].v[i]);
sol[v[1].v[i].destinatie] = v[1].v[i].cost;
}
make_heap(h.begin(), h.end(), cmp);
viz[1] = true;
while(!h.empty())
{
arc = h.front();
pop_heap(h.begin(), h.end(), cmp);
h.pop_back();
if(arc.cost <= sol[arc.destinatie])
sol[arc.destinatie] = arc.cost;
if(!viz[arc.destinatie]){
viz[arc.destinatie] = true;
for(i=0; i< v[arc.destinatie].v.size(); i++)
{
if(!viz[v[arc.destinatie].v[i].destinatie])
{
v[arc.destinatie].v[i].cost += arc.cost;
h.push_back(v[arc.destinatie].v[i]);
push_heap(h.begin(), h.end(), cmp);
}
}}
}
for(i=2; i<=n; i++)
if(sol[i] == oo)
printf("0 ");
else printf("%d ", sol[i]);
}
else{
read();
bell();}
return 0;
}