Pagini recente » Cod sursa (job #2551530) | Cod sursa (job #1358309) | Cod sursa (job #535463) | Cod sursa (job #2945026) | Cod sursa (job #2515822)
#include <iostream>
#include <fstream>
#include <set>
#include <vector>
#include <algorithm>
#define NMAX 50005
#define INF (1 << 30)
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
int N, M;
vector < pair <int , int> > Add[NMAX];
vector <int> dist(NMAX,INF);
set < pair <int , int> > st;
set < pair <int , int> > :: iterator it;
void shortPath(int start)
{
int lgx;
int currNode, weight;
st.insert(make_pair(0,1));
dist[start] = 0;
while(!st.empty())
{
it = st.begin();
st.erase(st.begin());
currNode = (*it).second;
weight = (*it).first;
if(weight > dist[currNode])
continue;
for(int i = 0; i < Add[currNode].size(); i++){
if(dist[Add[currNode][i].second] > dist[currNode] + Add[currNode][i].first){
dist[Add[currNode][i].second] = dist[currNode] + Add[currNode][i].first;
st.insert(make_pair(dist[Add[currNode][i].second],Add[currNode][i].second));
}
}
}
}
int main()
{
int x, y, w;
fin >> N >> M;
for(int i = 1; i <= M; i++){
fin >> x >> y >> w;
Add[x].push_back({w,y});
}
shortPath(1);
for(int i = 2; i <= N; i++){
fout << dist[i] << " ";
}
return 0;
}