Pagini recente » Cod sursa (job #2095843) | Cod sursa (job #73522) | Cod sursa (job #1779059) | Cod sursa (job #1970897) | Cod sursa (job #2686080)
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9+5;
ifstream in("dijkstra.in");
ofstream out("dijkstra.out");
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;
vector<pair<int,int>> graf[100005];
int n,m,dist[100005],viz[100005];
int main()
{
in>>n>>m;
for (int i=1;i<n;i++)
dist[i] = INF;
for(int i=0;i<m;i++)
{
int a,b,c;
in>>a>>b>>c;
graf[a-1].push_back({b-1,c});
}
pq.push({0,0});
while(!pq.empty())
{
int x = pq.top().second;
pq.pop();
if(viz[x])
continue;
viz[x] = 1;
for (auto it:graf[x])
{
int v = it.first;
int weight = it.second;
if (dist[v] > dist[x] + weight)
{
dist[v] = dist[x] + weight;
pq.push(make_pair(dist[v], v));
}
}
}
for (int i=1;i<n;i++)
if(dist[i] == INF)
out<<0;
else
out<<dist[i]<<' ';
}