Pagini recente » Cod sursa (job #2040958) | Cod sursa (job #1582650) | Cod sursa (job #745290) | Cod sursa (job #233909) | Cod sursa (job #2044091)
#include <bits/stdc++.h>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
const int NMAX=50005;
const int inf=LONG_MAX;
vector<pair<int,int> >L[NMAX];
priority_queue<pair<int,int> >Q;
int n,m,dist[NMAX];
bool viz[NMAX];
inline void READ()
{
fin>>n>>m;
while(m--)
{
int nod,nod1,cost;
fin>>nod>>nod1>>cost;
L[nod].push_back({nod1,cost});
}
}
inline void DIJKSTRA()
{
for(int i=2;i<=n;i++)
dist[i]=inf;
Q.push({0,1});
while(!Q.empty())
{
int nod=Q.top().second;
Q.pop();
if(!viz[nod])
{
viz[nod]=true;
for(auto i:L[nod])
{
if(dist[i.first]>dist[nod]+i.second)
{
dist[i.first]=dist[nod]+i.second;
Q.push({-dist[i.first],i.first});
}
}
}
}
cout<<n<<"\n";
for(int i=2;i<=n;i++)
if(dist[i]==inf)
fout<<"0 ";
else fout<<dist[i]<<" ";
}
int main()
{
READ();
DIJKSTRA();
fin.close();
fout.close();
return 0;
}