Pagini recente » Cod sursa (job #2315035) | Cod sursa (job #3294785) | Cod sursa (job #1531476) | Cod sursa (job #2886398) | Cod sursa (job #3342744)
#include <fstream>
#include <queue>
#include <vector>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
const int NMAX = 50005;
const int INF = 1e9;
#define pii pair <int, int>
struct Muchie{
int to, cost;
};
vector <Muchie> g[NMAX];
int dist[NMAX];
void pre(int n){
for(int i = 1; i <= n; ++i){
dist[i]= INF;
}
}
void dijkstra(int st){
dist[st] = 0;
priority_queue <pii, vector <pii>, greater <pii>> pq;
pq.push({0, st});
while(!pq.empty()){
pii temp = pq.top();
int nod = temp.second;
int cost = temp.first;
pq.pop();
if(dist[nod] != cost){
continue;
}
for(int i = 0; i < g[nod].size(); ++i){
int to = g[nod][i].to;
int cost = g[nod][i].cost;
if(dist[to] > dist[nod] + cost){
dist[to] = dist[nod] + cost;
pq.push({dist[to], to});
}
}
}
}
void solve(){
int n, m;
fin >> n >> m;
for(int i = 1; i <= m; ++i){
int x, y, cost;
fin >> x >> y >> cost;
Muchie temp;
temp.to = y;
temp.cost = cost;
g[x].push_back(temp);
}
pre(n);
int start = 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;
}