Cod sursa(job #1862531)

Utilizator DoubleNyNinicu Cristian DoubleNy Data 30 ianuarie 2017 01:02:29
Problema Algoritmul lui Dijkstra Scor 70
Compilator cpp Status done
Runda Arhiva educationala Marime 1.69 kb
#include <bits/stdc++.h>
using namespace std;
#define ios ios_base::sync_with_stdio(false);cin.tie(0);
#define setnow clock_t tStart=clock();
#define time (double)(clock() - tStart)/CLOCKS_PER_SEC;
typedef long long ll;
typedef long long int lli;
typedef pair < int, int> dbl;
const int maxInt = 1e9;
const lli maxLong = 1e18*2;
//
int dist[50000];
vector < pair < int, int > > adj[50000];
priority_queue< pair <int, int> , vector < pair < int, int > > , greater < pair <int, int > > > heap;
int n, m;

int Dijkstra(int source){
    for(int i = 0; i < 50000; i++)
        dist[i] = maxInt;
    dist[source] = 0;
    heap.push( make_pair(0, source));
    while(!heap.empty()){
            int node = heap.top().second;
            int lng = heap.top().first;
            heap.pop();
            for(int i = 0; i < adj[node].size(); i++){
                    int to = adj[node][i].first;
                    int length = adj[node][i].second;
                    if(dist[to] > dist[node] + length){
                            dist[to] = dist[node] + length;
                            heap.push( make_pair (dist[to], to));
                    }
            }
    }
}
int main(){
            ifstream cin("dijkstra.in");
            ofstream cout("dijkstra.out");
            cin >> n >> m;
            for(int i = 0; i < m; i++){
                    int a, b, price;
                    cin >> a >> b >> price;
                    adj[a].push_back( make_pair(b,price));
            }
            Dijkstra(1);
            for(int i = 2; i <= n; i++)
                if(dist[i] >= maxInt)
                    cout << 0 << ' ';
                else
                    cout << dist[i] << ' ';
}