Pagini recente » Cod sursa (job #1672586) | Cod sursa (job #2860623) | Cod sursa (job #3187359) | Cod sursa (job #2266496) | Cod sursa (job #3271610)
#include <iostream>
#include <fstream>
#include <queue>
#include <vector>
#define inf 1e9
#define per pair<int,int>
using namespace std;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
priority_queue<per,vector<per>,greater<per> >h;
vector<per> v[50005];
int d[50005],i,n,m,x,y,z;
void dijkstra(int k)
{
int i,t,u,c;
for(i=1;i<=n;i++)
d[i]=inf;
d[k]=0;
h.push({0,k});
while(!h.empty())
{
t=h.top().second;
c=h.top().first;
h.pop();
if(d[t]!=c) continue;
for(i=0;i<v[t].size();i++)
{
u=v[t][i].second;
c=v[t][i].first;
if(d[t]+c<d[u])
{
d[u]=d[t]+c;
h.push({d[u],u});
}
}
}
}
int main()
{
f>>n>>m;
for(i=1;i<=n;i++)
{
f>>x>>y>>z;
v[x].push_back({z,y});
}
dijkstra(1);
for(i=2;i<=n;i++)
if(d[i]==inf) g<<0<<" ";
else
g<<d[i]<<" ";
return 0;
}