Pagini recente » Monitorul de evaluare | Profil Terminator | Cod sursa (job #3335669) | Cod sursa (job #3310673) | Cod sursa (job #3337876)
/******************************************************************************
Welcome to GDB Online.
GDB online is an online compiler and debugger tool for C, C++, Python, Java, PHP, Ruby, Perl,
C#, OCaml, VB, Swift, Pascal, Fortran, Haskell, Objective-C, Assembly, HTML, CSS, JS, SQLite, Prolog.
Code, Compile, Run and Debug online from anywhere in world.
*******************************************************************************/
#include <fstream>
#include <queue>
#include <vector>
#include <climits>
using namespace std;
ifstream cin("dijkstra.in");
ofstream cout("dijkstra.out");
int n,m;
int INF = INT_MAX/2;
int main()
{
cin>>n>>m;
vector<vector<pair<int,int>>>adj(n+1);
priority_queue<
pair<int,int>,
vector<pair<int,int>>,
greater<pair<int,int>>>Q;
int a,b,c;
for(int i=1;i<=m;i++)
{
cin>>a>>b>>c;
adj[a].push_back({b,c});
}
vector<int>dist(n+1,INF);
dist[1] = 0;
Q.push({dist[1],1});
while(!Q.empty())
{
int nod = Q.top().second;
int d = Q.top().first;
Q.pop();
if(dist[nod]!=d)
{
continue;
}
for(auto p:adj[nod])
{
int v = p.first;
int c = p.second;
if(dist[nod]+c<dist[v])
{
dist[v] = dist[nod]+c;
Q.push({dist[v],v});
}
}
}
for(int i=2;i<=n;i++)
{
if(dist[i]!=INF)
{
cout<<dist[i]<<" ";
}else
{
cout<<0;
}
}
return 0;
}