Pagini recente » Cod sursa (job #2531933) | Cod sursa (job #1734821) | Cod sursa (job #1994159) | Cod sursa (job #1424942) | Cod sursa (job #1490130)
#include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
#define inf 10000000
using namespace std;
int n,m,x,y,c,dist[50015];
vector <pair <int ,int > >v[50015];
priority_queue< pair <int, int > > q;
void read()
{
scanf("%d%d",&n,&m);
for (int i=2;i<=n;++i)
dist[i]=inf;
q.push(make_pair(0,1));
for (int i=1;i<=m;++i)
{
scanf("%d%d%d",&x,&y,&c);
v[x].push_back(make_pair(y,c));
}
}
void dijk()
{
while (!q.empty())
{
int y=q.top().second;
int c=-q.top().first;
q.pop();
for (vector <pair<int ,int > > :: iterator it=v[y].begin();it!=v[y].end();++it)
{
if (c+it->second < dist[it->first])
{
dist[it->first]=c+it->second;
q.push(make_pair(-dist[it->first],it->first));
}
}
}
for (int i=2; i<=n; ++i)
if (dist[i]==inf)
printf("0 ");
else printf("%d ",dist[i]);
}
int main()
{
freopen("dijkstra.in","r",stdin);
freopen("dijkstra.out","w",stdout);
read();
dijk();
return 0;
}