Mai intai trebuie sa te autentifici.
Cod sursa(job #2964692)
Utilizator | Data | 13 ianuarie 2023 18:00:20 | |
---|---|---|---|
Problema | Algoritmul lui Dijkstra | Scor | 20 |
Compilator | cpp-64 | Status | done |
Runda | Arhiva educationala | Marime | 1.22 kb |
#include <iostream>
#include <queue>
#include <vector>
#include <fstream>
#include<climits>
using namespace std;
ifstream f ("dijkstra.in");
ofstream g ("dijkstra.out");
int n,m;
vector< pair<int,int> >adiacenta[100001];
int dist[100001];
bool viz[100001];
priority_queue< pair<int,int>, vector< pair<int,int> >, greater< pair<int,int> > >q;
void alg()
{
q.push({0,1});
while(!q.empty())
{
int cost = q.top().first;
int curent = q.top().second;
q.pop();
if(viz[curent]== 1)
continue;
viz[curent] = 1;
for(auto x:adiacenta[curent])
{
int next = x.first;
int costnou = x.second;
if(dist[next]>costnou+cost)
{
q.push({cost+costnou,next});
dist[next] = costnou+cost;
}
}
}
}
int main()
{
f >> n >> m;
for(int i = 1;i<=m;i++)
{
int x,y,c;
f >> x >> y >> c;
adiacenta[x].push_back({y,c});
}
for(int i = 1; i<=50000; i++)
dist[i] = INT_MAX;
alg();
for(int i = 2;i<=n;i++)
{
g << dist[i]<< " ";
}
return 0;
}