Pagini recente » Cod sursa (job #2140024) | Cod sursa (job #2611924) | Cod sursa (job #1340837) | Cod sursa (job #7665) | Cod sursa (job #3039936)
#include <fstream>
#include <queue>
using namespace std;
ifstream cin ("dijkstra.in");
ofstream cout ("dijkstra.out");
struct element
{
int nod;
int d;
};
class Comparare
{
public :
bool operator() (element a,element b)
{
if (a.d < b.d)
return true;
else
return false;
}
};
priority_queue<element,vector<element>,Comparare> q;
vector < pair<int,int> > g[105];
int n;
int distanta[105];
void dijkstra (int incep)
{
for (int i=1;i<=n;i++)
distanta[i] = 2e9;
distanta[incep] = 0;
element aux;
aux.nod = incep;
aux.d = 0;
q.push(aux);
while (!q.empty())
{
aux = q.top();
q.pop();
int vf = aux.nod;
if (aux.d != distanta[vf])
continue;
for (auto muchie:g[vf])
{
if (distanta[muchie.first] > distanta[vf] + muchie.second)
{
distanta[muchie.first] = distanta[vf] + muchie.second;
aux.nod = muchie.first;
aux.d = distanta[vf] + muchie.second;
q.push(aux);
}
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int p,i,j,c;
cin >> n >> p;
while (cin >> i >> j >> c)
g[i].push_back({j,c});
dijkstra(p);
for (int i=1;i<=n;i++)
{
if (distanta[i] == 2e9)
{
cout << "-1" << ' ';
continue;
}
cout << distanta[i] << ' ';
}
return 0;
}