Pagini recente » Cod sursa (job #2769164) | Cod sursa (job #1693686) | Cod sursa (job #2348211) | Cod sursa (job #1516797) | Cod sursa (job #2418135)
#include <bits/stdc++.h>
using namespace std;
ifstream fin ("dijkstra.in");
ofstream fout ("dijkstra.out");
void read();
int n, start, dist[105];
priority_queue<pair<int,int>, vector<pair<int,int>>, less<pair<int,int>>>q;
vector<pair<int, int>> v[105];
int main()
{
read();
for(int i=1; i<=n; i++)
dist[i] = INT_MAX/2-1;
dist[start] = 0;
q.push({0, start});
while(!q.empty())
{
int cur = q.top().second;
q.pop();
for(auto it : v[cur])
if(dist[cur] + it.first < dist[it.second])
{
dist[it.second] = dist[cur] + it.first;
q.push({dist[it.second], it.second});
}
}
for(int i=1; i<=n; i++)
if(dist[i]!=INT_MAX/2-1)
fout << dist[i] << ' ';
else
fout << -1 << ' ';
return 0;
}
void read()
{
fin >> n >> start;
int x, y, cost;
while(fin >> x >> y >> cost)
v[x].push_back({cost, y});
}