Pagini recente » Cod sursa (job #1318709) | Cod sursa (job #926359) | Cod sursa (job #593260) | Cod sursa (job #2848394) | Cod sursa (job #1889426)
#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 setare_dist()
{
for(int i = 1; i <= n; ++i)
dist[i] = inf;
}
void dijkstra()
{
dist[1] = 0;
q.push(make_pair(0,1));
while(not q.empty())
{
int nod = q.top().second;
int dst = -q.top().first;
q.pop();
if(dst > dist[nod])
continue;
dist[nod] = dst;
for(vector<pair<int,int> >::iterator it = g[nod].begin(); it != g[nod].end(); ++it)
if(dist[it->first] > dist[nod] + it->second)
q.push(make_pair(-(dist[nod] + it->second),it->first));
}
}
void afish()
{
for(int i = 2; i <= n; ++i)
printf("%d ", dist[i] == inf ? 0 : dist[i]);
}
int main()
{
freopen("dijkstra.in", "r", stdin);
freopen("dijkstra.out", "w", stdout);
citire();
setare_dist();
dijkstra();
afish();
return 0;
}