Pagini recente » Cod sursa (job #583539) | Cod sursa (job #778822) | Cod sursa (job #1954799) | Cod sursa (job #1570374) | Cod sursa (job #3246463)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream cin("dijkstra.in");
ofstream cout("dijkstra.out");
#define ll long long
vector<vector<pair<ll, ll>>> a;
ll n, p = 1, weight, m;
ll d[50005];
priority_queue<ll> q;
void dijkstra()
{
q.push(p);
d[p] = 0;
while(!q.empty())
{
ll u = q.top();
q.pop();
for(int i = 0; i < a[u].size(); i++)
if(d[a[u][i].first]> d[u] + a[u][i].second)
d[a[u][i].first] = d[u] + a[u][i].second, q.push(a[u][i].first);
}
}
int main()
{
ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin>>n>>m;
a.resize(n + 5);
for(int i = 1; i <= n; i++)
d[i] = 1e15;
ll x, y;
for(int i = 1; i <= m; i++)
{
cin>>x>>y>>weight;
a[x].push_back({y, weight});
}
dijkstra();
for(int i = 1; i <= n; i++)
{
if(i != p)
{
if(d[i] != 1e15)
cout<<d[i]<<" ";
else
cout<<"0"<<" ";
}
}
return 0;
}