Pagini recente » Cod sursa (job #1424240) | Cod sursa (job #1593307) | Cod sursa (job #1755919) | Cod sursa (job #939925) | Cod sursa (job #1045477)
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#define NMAX 50001
#define INF 0x3f3f3f3f
#define node first
#define dist second
using namespace std;
int N, M, x1, x2, cost, i, nodeCt;
vector< pair<int, int> > G[NMAX];
vector< pair<int, int> >::iterator it;
int Cost[NMAX];
struct comp {
bool operator() (const int &X, const int &Y) {
return (Cost[X] > Cost[Y]);
}
};
priority_queue< int, vector<int>, comp > Q;
int main() {
freopen("dijkstra.in", "r", stdin);
freopen("dijkstra.out", "w", stdout);
scanf("%d%d", &N, &M);
while (M--) {
scanf("%d%d%d", &x1, &x2, &cost);
G[x1].push_back( make_pair(x2, cost) );
}
memset(Cost, INF, sizeof(Cost));
Cost[1] = 0;
Q.push(1);
while (!Q.empty()) {
nodeCt = Q.top();
Q.pop();
for (it = G[nodeCt].begin(); it != G[nodeCt].end(); ++it)
if (Cost[(*it).node] > Cost[nodeCt] + (*it).dist) {
Cost[(*it).node] = Cost[nodeCt] + (*it).dist;
Q.push((*it).node);
}
}
for (i = 2; i <= N; ++i)
if(Cost[i] != INF ) printf("%d ", Cost[i]);
else printf("0 ");
return 0;
}