Pagini recente » Cod sursa (job #3196262) | Cod sursa (job #408406) | Cod sursa (job #2408992) | Cod sursa (job #2572362) | Cod sursa (job #2533570)
#include <fstream>
#include <vector>
#include <queue>
#define nmax 50004
#define inf 5000000004
#define f first
#define s second
using namespace std;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
int n, m, a, b, c, dist[nmax];
bool seen[nmax];
vector<pair<int, int> > G[nmax];
priority_queue<pair<int, int> > pq;
int main()
{
f>>n>>m;
for(int i=1;i<=m;i++) {
f>>a>>b>>c;
G[a].push_back({b, c});
}
for(int i=2;i<=n;i++)
dist[i]=inf;
pq.push({0, 1});
while(!pq.empty()) {
int node=pq.top().s;
pq.pop();
if(seen[node])
continue;
seen[node]=1;
for(auto i:G[node])
if(dist[node]+i.s<dist[i.f]) {
dist[i.f]=dist[node]+i.s;
pq.push({-dist[i.f], i.f});
}
}
for(int i=2;i<=n;i++)
if(dist[i]==inf)
g<<0<<' ';
else
g<<dist[i]<<' ';
f.close();
g.close();
return 0;
}