Pagini recente » Cod sursa (job #2949171) | Cod sursa (job #2767264) | Cod sursa (job #3127910) | Cod sursa (job #2884761) | Cod sursa (job #3005104)
#include <fstream>
#include <algorithm>
#include <vector>
#include <queue>
#include <climits>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
const int NMAX = 50005;
int dist[NMAX];
vector < vector < pair <int,int> > > adjList;
priority_queue < pair <int,int> , vector < pair <int,int> >, greater < pair <int,int> > > pq;
int main()
{
int n,m;
fin>>n>>m;
adjList.resize(n + 1);
int x,y,z;
for(int i = 1;i <= m;i++)
{
fin>>x>>y>>z;
adjList[x].push_back({z,y});
if(i != 1)
dist[i] = INT_MAX;
}
for(auto u : adjList[1]){
dist[u.second] = u.first;
pq.push(u);
}
pair <int,int> cand;
while(!pq.empty()){
cand = pq.top();
pq.pop();
if(dist[cand.second] == cand.first)
for(auto u : adjList[cand.second]){
if(cand.first + u.first < dist[u.second]){
dist[u.second] = u.first + cand.first;
pq.push({dist[u.second],u.second});
}
}
}
for(int i = 2;i <= n;i++)
{
dist[i] %= INT_MAX;
fout<<dist[i]<<" ";
}
return 0;
}