Pagini recente » Cod sursa (job #727694) | Cod sursa (job #2725647) | Cod sursa (job #2555230) | Cod sursa (job #2385478) | Cod sursa (job #2812256)
#include <iostream>
#include <fstream>
#include <set>
#include <vector>
#include<climits>
using namespace std;
ifstream f ("dijkstra.in");
ofstream g ("dijkstra.out");
int n,m,x,y,c;
bool viz[50001];
vector<pair<int,int>>adiacenta[50001];
int costuri[50001];
set<pair<int,int>> s;
void citire()
{
f >> n >> m;
for(int i = 1; i<=m; i++)
{
f >> x >> y >> c;
adiacenta[x].push_back({y,c});
}
for(int i = 1; i<=50000; i++)
costuri[i] = INT_MAX;
}
void alg()
{
costuri[1] = 1;
s.insert({1,1});
while(!s.empty())
{
int nod = s.begin()->second;
int cost = s.begin()->first;
s.erase(s.begin());
for(auto x:adiacenta[nod])
{
if(costuri[x.first]>x.second+cost)
{
//daca deja l-am modificat
// inseamna ca deja i am pus valoarea minima asa ca stergem daca mai exista un alt nod la fel
if(costuri[x.first]!=INT_MAX)
s.erase(s.find({costuri[x.first],x.first}));
costuri[x.first]=x.second+cost;
s.insert({costuri[x.first],x.first});
}
}
}
}
void afisare()
{
for(int i = 2; i<=n; i++)
{
if (costuri[i] == INT_MAX)
{
g << 0 << " ";
}
else
g << costuri[i]-1<< " ";
}
}
int main()
{
citire();
alg();
afisare();
}