Pagini recente » Cod sursa (job #2896346) | Cod sursa (job #1918441) | Cod sursa (job #2542803) | Clasament onis-2014-runda-1 | Cod sursa (job #1284017)
#include <cstdio>
#include <list>
#include <queue>
#include <algorithm>
#define inf 0x3f3f3f3f
using namespace std;
int d[50005],n,m;
list<pair<int,int> > g[50005];
queue<pair<int,int> > h;
void read()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=m;++i)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
g[a].push_back(make_pair(c,b));
}
for(int i=1;i<=n;++i)
d[i]=inf;
}
void solve(int k)
{
d[k]=0;
h.push(make_pair(0,k));
while(!h.empty())
{
k=h.front().second;
h.pop();
for(list<pair<int,int> >::iterator it=g[k].begin();it!=g[k].end();++it)
if(d[it->second]>d[k]+it->first)
{
d[it->second]=d[k]+it->first;
h.push(make_pair(d[it->second],it->second));
}
}
}
void print()
{
for(int i=2;i<=n;++i)
if(d[i]!=inf)
printf("%d ",d[i]);
else
printf("0 ");
}
int main()
{
freopen("file.in","r",stdin);
freopen("file.out","w",stdout);
read();
solve(1);
print();
return 0;
}