Pagini recente » Cod sursa (job #2774502) | Cod sursa (job #478206) | Cod sursa (job #2463759) | Cod sursa (job #2478201) | Cod sursa (job #2165555)
#include <bits/stdc++.h>
#define MA 2147483647
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
int m,n,a,b,c,viz[50001],dist[50001],tot;
vector<pair<int,int> > v[50001];
priority_queue<pair<int,int> > p;
int main()
{
fin>>n>>m;
tot=n;
for (int i=1;i<=m;i++)
{
fin>>a>>b>>c;
v[a].push_back(make_pair(b,c));
}
for (int i=1;i<=n;i++)
dist[i]=MA;
dist[1]=0;
p.push(make_pair(0,1));
while (tot>0)
{
auto t=p.top();
p.pop();
if (viz[t.second]==0)
{
tot--;
viz[t.second]=1;
for (auto vec:v[t.second])
{
if (-t.first+vec.second<dist[vec.first])
{
dist[vec.first]=-t.first+vec.second;
p.push(make_pair(-dist[vec.first], vec.first));
}
}
}
}
for (int i=2;i<=n;i++)
if (dist[i]!=MA)
fout<<dist[i]<<" ";
else fout<<0<<" ";
return 0;
}