Pagini recente » Cod sursa (job #1410772) | Cod sursa (job #1687034) | Cod sursa (job #738912) | Cod sursa (job #1421007) | Cod sursa (job #2275119)
#include <bits/stdc++.h>
using namespace std;
#define NMAX 50005
#define oo 0x3f3f3f3f
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
vector < pair< int, int > > G[NMAX];
priority_queue < pair <int, int>, vector < pair <int, int> >, greater < pair<int, int> > > q;
int dist[NMAX], i, n, m, x, y, cost;
bitset<NMAX>viz;
void dijkstra(int nodInceput)
{
memset(dist, oo, sizeof(dist));
dist[nodInceput] = 0;
q.push({dist[nodInceput], nodInceput});
while (!q.empty())
{
int nodCurent = q.top().second;
q.pop();
viz[nodCurent] = true;
int nrvecini = G[nodCurent].size();
for (i=0; i<nrvecini; i++)
{
int vecin = G[nodCurent][i].first;
if (G[nodCurent][i].second + dist[nodCurent] < dist[vecin])
dist[vecin] = G[nodCurent][i].second + dist[nodCurent];
if (!viz[vecin])
q.push({dist[vecin], vecin});
}
}
}
int main()
{
fin >> n >> m;
for (i=1; i<=m; i++)
{
fin >> x >> y >> cost;
G[x].push_back({y, cost});
}
dijkstra(1);
for (i=2; i<=n; i++)
{
if (dist[i] != oo)
fout << dist[i] << " ";
else
fout << 0 << " ";
}
return 0;
}