Pagini recente » Cod sursa (job #2863722) | Cod sursa (job #817476) | Cod sursa (job #2274647) | Cod sursa (job #2479847) | Cod sursa (job #1448096)
#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
#define nmx 50005
#define inf 0x3f3f3f3f
using namespace std;
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].push_back(make_pair(nod2,cost));
}
memset(dist, inf, sizeof(dist));
dist[1] = 0;
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(dist[it->first] > dist[nod] + it->second) {
dist[it->first] = dist[nod] + it->second;
q.push(make_pair(-dist[it->first],it->first));
}
}
for(int i = 2; i <= n; ++i)
printf("%d ", dist[i] == inf ? 0 : dist[i]);
return 0;
}