Pagini recente » Monitorul de evaluare | Cod sursa (job #3246243) | Cod sursa (job #409528) | Cod sursa (job #269508) | Cod sursa (job #3340463)
#include <bits/stdc++.h>
#define mod 1000000007
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 start)
{
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,start});
dist[start]=0;
while(!pq.empty())
{
int nod=pq.top().second;
int cost=pq.top().first;
pq.pop();
if(dist[nod] < cost)
continue;
for(auto it : L[nod])
if(dist[it.first] > dist[nod] + it.second)
{
dist[it.first] = dist[nod] + 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);
}