Pagini recente » Cod sursa (job #2199755) | Cod sursa (job #473734) | Cod sursa (job #2136928) | Cod sursa (job #521647) | Cod sursa (job #1562861)
#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
#define pb push_back
#define mp make_pair
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;
int main() {
freopen("dijkstra.in", "r", stdin);
freopen("dijkstra.out", "w", stdout);
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].pb(mp(nod2,cost));
}
memset(dist,inf,sizeof(dist));
dist[1] = 0;
q.push(mp(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(dist[it->first] > dist[nod] + it->second) {
dist[it->first] = dist[nod] + it->second;
q.push(mp(-dist[it->first],it->first));
}
}
for(int i = 2; i <= n; ++i)
printf("%d ", dist[i] == inf ? 0 : dist[i]);
return 0;
}