Pagini recente » Cod sursa (job #3217338) | Cod sursa (job #2162370) | Cod sursa (job #3267876) | Cod sursa (job #338308) | Cod sursa (job #2574873)
#include <bits/stdc++.h>
#define FILE_NAME "dijkstra"
#define fast ios_base :: sync_with_stdio(0); cin.tie(0);
#pragma GCC optimize("O3")
#define NMAX 50000 + 100
using namespace std;
ifstream f(FILE_NAME ".in");
ofstream g(FILE_NAME ".out");
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pi;
typedef pair<ll,ll> llp;
typedef pair<ld,ld> pct;
const int inf = 1 << 30;
const ll mod = 1e9 + 7;
const ld eps = 1e-9;
void add(ll &a , ll b)
{
a += b;
a %= mod;
}
void sub(ll &a, ll b)
{
a = (a - b + mod) % mod;
}
int n, m, dist[NMAX], x, y, z;
vector <pi> G[NMAX];
///Incercare cu priority queue
void Dijkstra(int start)
{
priority_queue<pi, vector<pi>, greater<pi> > Q;
for(int i = 1; i <= n; ++i)
dist[i] = inf;
dist[start] = 0;
Q.push({0,start});
while(!Q.empty())
{
int nod = Q.top().second;
Q.pop();
for(auto it : G[nod])
{
int urm = it.first;
int cost = it.second;
if(dist[urm] > dist[nod] + cost)
{
dist[urm] = dist[nod] + cost;
Q.push({dist[urm], urm});
}
}
}
for(int i = 1; i <= n; ++i)
if(i != start)
{
if(dist[i] != inf)
g << dist[i] << ' ';
else
g << 0 << ' ';
}
}
int main()
{
f >> n >> m;
for(int i = 1; i <= m; ++i)
{
f >> x >> y >> z;
G[x].push_back({y,z});
}
Dijkstra(1);
f.close();
g.close();
return 0;
}