Pagini recente » Cod sursa (job #186097) | Cod sursa (job #845924) | Cod sursa (job #2945553) | Cod sursa (job #189370) | Cod sursa (job #2958258)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
int n,m,dist[50003];
vector<pair<int,int>>a[50003];
void dijkstra(int s)
{
for(int i=2;i<=n;i++)
dist[i]=1e9;
priority_queue<pair<int,int>, vector<pair<int,int>>, greater<>> q;
q.push({0,s});
while(!q.empty())
{
auto x=q.top();
q.pop();
for(auto it: a[x.second])
{
if(dist[it.first]>x.first+it.second)
{
dist[it.first]=x.first+it.second;
q.push({dist[it.first],it.first});
}
}
}
for(int i=2;i<=n;i++)
if(dist[i]==1e9)
g<<0<<" ";
else
g<<dist[i]<<" ";
}
int main()
{
f>>n>>m;
for(int i=1;i<=m;i++)
{
int X,Y,Z;
f>>X>>Y>>Z;
a[X].push_back({Y,Z});
}
dijkstra(1);
return 0;
}