Pagini recente » Cod sursa (job #1136210) | Cod sursa (job #446191) | Cod sursa (job #2830827) | Cod sursa (job #1030521) | Cod sursa (job #3273635)
#include <fstream>
#include <set>
#include <vector>
#define int long long
using namespace std;
const int INF = 1e18;
const int NMAX =50002;
ifstream in("dijkstra.in");
ofstream out("dijkstra.out");
vector <pair<int, int> > adj[NMAX + 2];
int n, source, m;
void dijkstra(){
vector <int> v;
v.assign(NMAX, INF);
vector <bool> visited(n, false);
v[source] = 0;
for(int i = 1;i<=n;i++){
int nod = -1;
for(int j= 1;j<=n;j++){
if(!visited[j] && (nod == -1 || v[j] < v[nod])){
nod = j;
}
}
if(v[nod] == INF){
break;
}
visited[nod] = true;
for(auto vecin : adj[nod]){
int to = vecin.first;
int cost = vecin.second;
if(v[nod] + cost < v[to]){
v[to] = v[nod] + cost;
}
}
}
for(int i = 2;i<=n;i++){
if(v[i] == INF){
out<<"0 ";
}else{
out<<v[i]<<" ";
}
}
}
signed main(){
int from, to, cost;
in>>n>>m;
source = 1;
for(int i = 1;i<=n;i++){
in>>from>>to>>cost;
adj[from].push_back(make_pair(to, cost));
}
dijkstra();
return 0;
}