Pagini recente » Cod sursa (job #3305092) | Cod sursa (job #2802510) | Cod sursa (job #1357535) | Cod sursa (job #695961) | Cod sursa (job #2802620)
#include <fstream>
#include <vector>
#include <queue>
#include <utility>
using namespace std;
ifstream in("dijkstra.in");
ofstream out("dijkstra.out");
const int oo = 2e9;
int n, d[50001];
bool f[50001];
priority_queue<int, vector<int>, greater<int>> q;
vector<pair<int, int>> g[50001];
void read()
{
int x, y, c, m;
in >> n >> m;
while(m--)
in >> x >> y >> c,
g[x].push_back({y, c});
}
void dijkstra()
{
for(int i = 2; i <= n; ++i)
d[i] = oo;
q.push(1);
f[1] = 1;
int x;
while(!q.empty())
{
x = q.top();
f[x] = 0;
q.pop();
for(auto i : g[x])
if(d[x] + i.second < d[i.first])
{
d[i.first] = d[x] + i.second;
if(f[i.first] == 0)
q.push(i.first),
f[i.first] = 1;
}
}
}
void afis()
{
for(int i = 2; i <= n; ++i)
out << (d[i] != oo ? d[i] : 0) << ' ';
}
int main()
{
read();
dijkstra();
afis();
return 0;
}