Pagini recente » Cod sursa (job #2059959) | Cod sursa (job #1706144) | Cod sursa (job #2336268) | Cod sursa (job #2863054) | Cod sursa (job #3275218)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
#define pii pair<int, int>
const int NMAX = 50005;
const int INF = 1e9;
struct Muchie{
int to, cost;
};
vector <Muchie> g[NMAX];
priority_queue <pii, vector <pii>, greater <pii>> pq;
int dist[NMAX];
int pred[NMAX];
int n;
void dijkstra(int start){
dist[start] = 0;
pq.push({0, start});
while(!pq.empty()){
pii temp = pq.top();
pq.pop();
if(dist[temp.second] > temp.first){
continue;
}
for(auto edge:g[temp.second]){
if(dist[edge.to] > dist[temp.second] + edge.cost){
dist[edge.to] = dist[temp.second] + edge.cost;
pred[edge.to] = temp.second;
pq.push({dist[edge.to], edge.to});
}
}
}
}
void solve(){
int start = 1;
int u, w, c, m;
fin >> n >> m;
for(int i = 1; i <= m; ++i){
fin >> u >> w >> c;
Muchie temp;
temp.to = w;
temp.cost = c;
g[u].push_back(temp);
}
for(int i = 1; i <= n; ++i){
dist[i] = INF;
pred[i] = -1;
}
dijkstra(start);
for(int i = 2; i <= n; ++i){
if(dist[i] == INF){
fout << 0;
}
else{
fout << dist[i] << " ";
}
}
}
int main()
{
solve();
return 0;
}