Pagini recente » Cod sursa (job #2131315) | Cod sursa (job #2806363) | Cod sursa (job #2545103) | Cod sursa (job #1166425) | Cod sursa (job #2574859)
#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];
void Dijkstra(int start)
{
set <pi> Q;
for(int i = 1; i <= n; ++i)
dist[i] = inf;
dist[start] = 0;
Q.insert({0,start});
while(!Q.empty())
{
int nod = (*Q.begin()).second;
Q.erase(Q.begin());
for(auto it : G[nod])
{
int urm = it.first;
int cost = it.second;
if(dist[urm] > dist[nod] + cost)
{
if(dist[urm] != inf)
Q.erase(Q.find({dist[urm],urm}));
dist[urm] = dist[nod] + cost;
Q.insert({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;
}