Pagini recente » Cod sursa (job #3226947) | Cod sursa (job #620844) | Infoarena Monthly 2012, Runda 7 | Cod sursa (job #737047) | Cod sursa (job #1699117)
#include <iostream>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
const int N = 1 + 5e4, inf = 0x3f3f3f3f;
struct Node{
int y, c;
Node(int y, int c) : y(y), c(c) {}
inline bool operator<(const Node& N) const {
return c > N.c;
}
};
vector<Node> graph[N];
int dist[N], n;
bool use[N];
void dijkstra(int x){
priority_queue<Node> H;
H.push( Node(x, 0) );
memset(dist, inf, sizeof(dist));
memset(use, false, sizeof(use));
dist[x] = 0;
while ( !H.empty() ){
x = H.top().y; H.pop();
if (use[x])
continue;
use[x] = true;
for (Node e : graph[x])
if ( dist[x] + e.c < dist[e.y] ){
dist[e.y] = dist[x] + e.c;
H.push( Node(e.y, dist[e.y]) );
}
}
}
int main(){
freopen("dijkstra.in", "r", stdin);
freopen("dijkstra.out", "w", stdout);
int m, x, y, c;
cin >> n >> m;
while (m--){
cin >> x >> y >> c;
graph[x].push_back( Node(y, c) );
}
dijkstra(1);
for (int i = 2; i <= n; i++)
cout << (dist[i] != inf ? dist[i] : 0) << ' ';
cout << '\n';
return 0;
}