Pagini recente » Cod sursa (job #31725) | Cod sursa (job #31676) | Cod sursa (job #302867) | Cod sursa (job #3143183) | Cod sursa (job #1354731)
#include <stdio.h>
#include <vector>
#include <queue>
#include <utility>
using namespace std;
#define Nmax 50005
#define inf 2500005
vector < pair <int, int> > graph[Nmax];
vector < pair <int, int> >::iterator it;
int dist[Nmax];
bool done[Nmax];
int n, m;
struct compare{
bool operator() (int i, int j){
return (dist[i] > dist[j]) ? 1 : 0;
}
};
priority_queue <int, vector <int>, compare> heap;
void read()
{
int x, y, c;
freopen("dijkstra.in", "r", stdin);
scanf("%d %d", &n, &m);
for (int i = 1; i <= m; ++i){
scanf("%d %d %d", &x, &y, &c);
graph[x].push_back(make_pair(y, c));
}
fclose(stdin);
}
void dijkstra()
{
int top;
for (int i = 2; i <= n; ++i)
dist[i] = inf;
heap.push(1);
while (!heap.empty()){
top = heap.top();
heap.pop();
if (done[top]) continue;
done[top] = 1;
for (it = graph[top].begin(); it != graph[top].end(); ++it)
if ((dist[it->first] > dist[top] + it->second)){
dist[it->first] = dist[top] + it->second;
heap.push(it->first);
}
}
}
void write()
{
freopen("dijkstra.out", "w", stdout);
for (int i = 2; i <= n; ++i)
printf("%d ", (dist[i] == inf) ? 0 : dist[i]);
fclose(stdout);
}
int main()
{
read();
dijkstra();
write();
return 0;
}