Pagini recente » Cod sursa (job #181250) | Cod sursa (job #2240305) | Cod sursa (job #3185841) | Cod sursa (job #3036652) | Cod sursa (job #2196757)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream cin("dijkstra.in");
ofstream cout("dijkstra.out");
const int MAX = 50001, INF = (1 << 30);
int n, d[MAX], m, sel[MAX];
vector<pair<int, int> > adj[MAX];
class mycomparison
{
public:
bool operator() (const pair<int, int> &lhs, const pair<int, int> &rhs) const
{
return (lhs.second > rhs.second);
}
};
void dijkstra(int s) {
for(int i = 1; i <= n; ++i) {
d[i] = INF;
}
d[s] = 0;
priority_queue<pair<int, int>, vector<pair<int, int> >, mycomparison> pq;
pq.push({s, d[s]});
while(!pq.empty()) {
int u = pq.top().first;
int dist = pq.top().second;
pq.pop();
if(dist > d[u]) continue;
for(auto it: adj[u]) {
int v = it.first;
int w = it.second;
if(sel[v] == 0 && d[v] > d[u] + w) {
d[v] = d[u] + w;
pq.push({v, d[v]});
}
}
}
for(int i = 1; i <= n; ++i) {
if(d[i] == INF)
d[i] = 0;
}
}
int main()
{
/*
ifstream f1("dijkstra.out");
ifstream f2("out.ok");
ofstream cout("debug.out");
for(int i = 1; i <= 50000; ++i) {
int x, y;
f1 >> x; f2 >> y;
if(x != y) {
cout << i << ' ' << x << ' ' << y << endl;
}
}
return 0;
*/
cin >> n >> m;
for(int i = 1; i <= m; ++i) {
int u, v, w;
cin >> u >> v >> w;
adj[u].push_back({v, w});
}
dijkstra(1);
for(int i = 2; i <= n; ++i) {
cout << d[i] << ' ';
}
return 0;
}