Pagini recente » Cod sursa (job #1376450) | Cod sursa (job #1648096) | Cod sursa (job #1452051) | Cod sursa (job #3157838) | Cod sursa (job #1889399)
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
const int nmx = 50002;
const int inf = 0x3f3f3f3f;
int n,m,dist[nmx];
vector <pair<int,int> > g[nmx];
priority_queue <pair<int,int> > q;
void citire()
{
scanf("%d %d", &n, &m);
for(int i = 1; i <= m; ++i)
{
int nod1,nod2,cost;
scanf("%d %d %d", &nod1, &nod2, &cost);
g[nod1].push_back(make_pair(nod2,cost));
}
}
void dijkstra()
{
dist[1] = 1;
q.push(make_pair(0,1));
while(not q.empty())
{
int nod = q.top().second;
q.pop();
for(vector<pair<int,int> >::iterator it = g[nod].begin(); it != g[nod].end(); ++it)
if(not dist[it->first])
{
dist[it->first] = dist[nod] + it->second;
q.push(make_pair(-dist[it->first],it->first));
}
}
}
void afish()
{
for(int i = 2; i <= n; ++i)
printf("%d ", dist[i] == 0 ? 0 : dist[i] - 1);
}
int main()
{
freopen("dijkstra.in", "r", stdin);
freopen("dijkstra.out", "w", stdout);
citire();
dijkstra();
afish();
return 0;
}