Pagini recente » Cod sursa (job #3190589) | Cod sursa (job #1672241) | Cod sursa (job #606722) | Cod sursa (job #1551084) | Cod sursa (job #2732971)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
const int N = 50001;
const int INF = 1e9 + 100;
vector <pair <int,int>> a[N];
priority_queue <pair <int,int>> pq;
int d[N],n,m;
bool este_prelucrat[N];
ifstream in("dijkstra.in");
ofstream out("dijkstra.out");
void dijkstra(int x0)
{
//initializez d
for(int i = 1; i <= n; i++)
{
d[i] = INF;
}
d[x0] = 0;
pq.push({-d[x0],x0});
while(!pq.empty())
{
int x = pq.top().second;
pq.pop();
if(este_prelucrat[x])
{
continue;
}
este_prelucrat[x] = 1;
//actualizam distantele fata de succesorii lui x
for(auto p:a[x])
{
int y = p.first;
int c = p.second;
if(d[x] + c < d[y])
{
d[y] = d[x] + c;
pq.push({-d[y],y});
}
}
}
}
int main()
{
in >> n >> m;
for(int i = 1; i <= m; i++)
{
int x,y,c;
in >> x >> y >> c;
a[x].push_back({y,c});
}
dijkstra(1);
for(int i = 2; i <= n; i++)
{
if(d[i] == INF)
{
d[i] = 0;
}
out << d[i] << " ";
}
in.close();
out.close();
return 0;
}