Pagini recente » Cod sursa (job #3337876) | Cod sursa (job #3330508) | Cod sursa (job #1848622) | Cod sursa (job #87466) | Cod sursa (job #3340263)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
int n,m;
vector<pair<int,int>>L[50002];
int dist[50002];
void Dijkstra(int nod)
{
for(int i=1;i<=n;++i)
dist[i]=2e9;
priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>>pq;
pq.push({0,nod});
dist[nod]=0;
while(!pq.empty())
{
int n=pq.top().second;
int c=pq.top().first;
pq.pop();
if(dist[n] < c)
continue;
for(auto it : L[n])
{
if(dist[it.first] > dist[n] + it.second)
{
dist[it.first] = dist[n] + it.second;
pq.push({dist[it.first],it.first});
}
}
}
for(int i=2;i<=n;++i)
{
if(dist[i] == 2e9)
fout<<0<<' ';
else
fout<<dist[i]<<' ';
}
}
int main()
{
fin>>n>>m;
for(int i=1;i<=m;++i)
{
int x,y,c;
fin>>x>>y>>c;
L[x].push_back({y,c});
L[y].push_back({x,c});
}
Dijkstra(1);
}