Pagini recente » Cod sursa (job #3243885) | Cod sursa (job #1352947) | Cod sursa (job #2451857) | Cod sursa (job #2452398) | Cod sursa (job #2476864)
#include <bits/stdc++.h>
#define NMAX 50001
#define INFNITY 1800000000000
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
int n, m;
vector<vector<pair<int, int>>> graph(NMAX, vector<pair<int, int>>());
vector<bool> visited(NMAX, false);
vector<long long> dist(NMAX, INFNITY);
struct comp{
bool operator ()(int& A, int &B)
{
if(dist[A] > dist[B]) return true;
return false;
}
};
priority_queue<int, vector<int>, comp> pq;
int main()
{
fin >> n >> m;
for(int x, y, c, i = 1; i <= m; ++i)
{
fin >> x >> y >> c;
graph[x].push_back(make_pair(y, c));
}
dist[1] = 0;
pq.push(1);
while(!pq.empty())
{
int node = pq.top();
pq.pop();
if(visited[node] == true) continue;
visited[node] = true;
for(auto& next : graph[node])
{
if(dist[node] + next.second < dist[next.first])
{
dist[next.first] = dist[node] + next.second;
pq.push(next.first);
}
}
}
for(int i = 2; i <= n; ++i)
{
if(dist[i] == INFNITY) fout << "0 ";
else fout << dist[i] << " ";
}
}