Pagini recente » Cod sursa (job #956365) | Cod sursa (job #1074929) | Cod sursa (job #1789759) | Cod sursa (job #119902) | Cod sursa (job #2348251)
#include <bits/stdc++.h>
using namespace std;
ifstream in("dijkstra.in");
ofstream out("dijkstra.out");
int dist[50005];
bool viz[50005];
vector< pair<int,int> > v[50005];
priority_queue< pair<int,int> > q;
int main()
{
int n,m;
in >> n >> m;
for (int i = 1; i<=m; i++)
{
int a,b,val;
in >> a >> b >> val;
v[a].push_back({b,val});
}
for (int i = 2; i<=n; i++)
dist[i] = 1<<30;
q.push({0,1});
while (!q.empty())
{
int now = q.top().second;
q.pop();
if (viz[now])
continue;
viz[now] = 1;
for (auto it: v[now])
{
if (it.second+dist[now]<dist[it.first])
{
dist[it.first] = it.second+dist[now];
q.push({-dist[it.first],it.first});
}
}
}
for (int i = 2; i<=n; i++)
if (dist[i] == 1<<30)
out << "0 ";
else
out << dist[i] << " ";
}