Pagini recente » Cod sursa (job #2184299) | Cod sursa (job #1666517) | Cod sursa (job #1957972) | Cod sursa (job #3338039) | Cod sursa (job #3319469)
/******************************************************************************
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<climits>
#include<functional>
#include<vector>
#include<algorithm>
#include<map>
using namespace std ;
ifstream cin("dijkstra.in");
ofstream cout("dijkstra.out");
int main()
{
int n , m , x , y , c ;
cin>>n>>m;
vector<vector<pair<int,int> > > adj(n+1);
for(int i = 1 ; i <= m ; i ++ ){
cin>>x>>y>>c;
adj[x].push_back(make_pair(y,c));
}
vector<int> dist(n+1,INT_MAX);
priority_queue<pair<int,int> , vector<pair<int,int> > , greater<pair<int,int> > > pq ;
dist[1] = 0 ;
pq.push(make_pair(dist[1],1));
while(!pq.empty()){
pair<int,int> nr = pq.top() ;
pq.pop() ;
int nod = nr.second ;
for(int j = 0 ; j < adj[nod].size() ; j ++ ){
pair<int,int> nei = adj[nod][j];
int v = nei.first ;
int weight = nei.second ;
if(dist[v] > dist[nod] + weight ){
dist[v] = dist[nod] + weight ;
pq.push(make_pair(dist[v],v));
}
}
}
for(int i = 2 ; i <= n ; i ++ ){
if(dist[i] == INT_MAX)
dist[i] = 0 ;
cout<<dist[i]<< ' ';
}
return 0;
}