Pagini recente » Cod sursa (job #1328619) | Cod sursa (job #125001) | Cod sursa (job #1247928) | Cod sursa (job #2890568) | Cod sursa (job #1700343)
#include <iostream>
#include <cstring>
#include <vector>
#include <set>
using namespace std;
const int N = 1 + 5e4, inf = 0x3f3f3f3f;
struct Node{
int y, c;
Node(int y, int c) : y(y), c(c) {}
};
vector<Node> graph[N];
set< pair<int, int> > S;
int dist[N], n;
inline void push(int x, int d){
if ( dist[x] < d ) return;
S.erase( make_pair(dist[x], x) );
S.emplace(dist[x] = d, x);
}
inline int pop(){
int x = S.begin() -> second;
S.erase( S.begin() );
return x;
}
void dijkstra(int x){
memset(dist, inf, sizeof(dist));
push(x, 0);
while ( !S.empty() ){
x = pop();
for (Node e : graph[x])
push(e.y, dist[x] + e.c);
}
}
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;
}