Pagini recente » Statistici Motoc George (Motoc00) | Istoria paginii utilizator/emma1997 | Cod sursa (job #1574444) | Cod sursa (job #123046) | Cod sursa (job #2990564)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#include <bitset>
#include <tuple>
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
const int NMAX = 200005, INF=INT_MAX;
vector <pair<int, int> > G[NMAX];
priority_queue<pair<int, int> > pq;
bitset <NMAX> viz;
int n, m, s, tata[NMAX], d[NMAX];
void citire()
{
fin>>n>>m;
for(int i=0; i<m; ++i)
{
int x, y, c;
fin>>x>>y>>c;
G[x].push_back(make_pair(y, c));
}
for(int i=1; i<=n; ++i)
d[i]=INF;
d[1]=0;
}
void apm()
{
pq.push(make_pair(0, 1));
while(!pq.empty())
{
int nod=pq.top().second;
int cost=-pq.top().first;
pq.pop();
if(viz[nod])
continue;
viz[nod]=1;
for(auto nbr: G[nod])
{
if(cost+nbr.second<d[nbr.first]) d[nbr.first]=cost+nbr.second;
if(!viz[nbr.first])
pq.push(make_pair(cost-nbr.second, nbr.first));
}
}
for(int i=2; i<=n; ++i)
fout<<d[i]<<" ";
}
int main()
{
citire();
apm();
}