Pagini recente » Cod sursa (job #929472) | Cod sursa (job #899671) | Cod sursa (job #1231556) | Cod sursa (job #1594281) | Cod sursa (job #3285594)
#include <iostream>
#include <fstream>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
using namespace std;
string filename = "dijkstra";
ifstream in(filename + ".in");
ofstream out(filename + ".out");
const int N = 50000;
const int INF = 1 << 30;
struct succesor{
int vf,c;
};
vector <succesor> s[N+1];
vector <int> d;
vector <bool> vis;
int n, m;
void dijkstra(int x0)
{
d.resize(n+1, INF);
vis.resize(n+1, false);
priority_queue<pair<int,int>, vector<pair<int,int>>, greater<pair<int,int>>> h;
d[x0] = 0;
h.push({0, x0});
while(!h.empty())
{
pair<int, int> p = h.top();
h.pop();
int c = p.first, x = p.second;
if(vis[x])
continue;
vis[x] = true;
for(auto next : s[x])
{
int y = next.vf;
int c = next.c;
if(d[x] + c < d[y])
{
d[y] = d[x] + c;
h.push({d[y], y});
}
}
}
}
int main()
{
in>>n>>m;
for(int i = 1; i <= m; i++)
{
int x, y, c;
in >> x >> y >> c;
s[x].push_back((succesor) {y,c});
}
dijkstra(1);
for(int i = 2; i<=n;i++)
{
if(d[i] == INF)
d[i]=0;
out<<d[i]<<' ';
}
return 0;
}