Pagini recente » Cod sursa (job #3164975) | Cod sursa (job #2132115) | Cod sursa (job #796950) | Cod sursa (job #2559780) | Cod sursa (job #2980526)
#include <fstream>
#include <iostream>
#include <vector>
#include <queue>
using namespace std;
ifstream f("dijkstra.in");
ofstream g("dijkstra.out");
int n, m;
int dist[50010];
int inqueue[50010];
int inf = 2 * 1e9;
struct cmp{
bool operator()(int a, int b)
{
return dist[a] > dist[b];
}
};
vector < pair < int , int > > graf[50005];
priority_queue < int, vector <int> , cmp> pq;
void read()
{
f >> n >> m;
for(int i = 0; i < m; ++i)
{
int x, y, c;
f >> x >> y >> c;
graf[x].push_back({y, c});
}
}
void dijkstra()
{
dist[1] = 0;
inqueue[1] = true;
pq.push(1);
fill(dist + 2, dist + n + 1, inf);
while(!pq.empty())
{
int nod = pq.top();
pq.pop();
inqueue[nod] = 0;
for(auto t : graf[nod])
{
if(dist[nod] + t.second < dist[t.first])
{
dist[t.first] = dist[nod] + t.second;
if(!inqueue[t.first])
{
inqueue[t.first] = 1;
pq.push(t.first);
}
}
}
}
}
int main()
{
read();
dijkstra();
for(int i = 2; i <= n; ++i)
if(dist[i] != inf)
g << dist[i] << ' ';
else
g << 0 << ' ';
return 0;
}