Cod sursa(job #1990656)

Utilizator ajeccAjechiloae Eugen ajecc Data 12 iunie 2017 22:10:53
Problema Algoritmul lui Dijkstra Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 1.72 kb
#include <bits/stdc++.h>
#define for0(i, n) for(int i = 0; i < n; i++)
#define for1(i, n) for(int i = 1; i <= n; i++)
#define pb push_back
#define mp make_pair
#define all(v) v.begin(), v.end()
#define V vector<int>
#define VP vector<pair<int, int> >
#define clr(A, x) memset(A, x, sizeof(A))
#define cpy(A, B) memcpy(A, B, sizeof(B))
#define g(s) getline(cin, s) ///ai grija la fin/cin ///
#define FASTIO ios_base::sync_with_stdio(0)
const long long INFLL = 2 * 1e18 + 100;
const int INFINT = 2 * 1e9 + 100;
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");
void die()
{
    cout << "-1";
    exit(0);
}
const int NMAX = 5 * 1e4 + 5;
const int MOD = 1e9 + 7; /// careful here (7 or 9, 66.. etc)
const double PI = atan(1) * 4;
const double EPS = 1e-12;

int n, m;
VP graf[NMAX];
int d[NMAX];
priority_queue<pair<int, int>, VP, greater<pair<int, int> > > coada; // cost - nod

void dijkstra()
{
    while(!coada.empty())
    {
        int nod = coada.top().second;
        int cost = coada.top().first;
        coada.pop();

        if(d[nod] > cost)
        {
            d[nod] = cost;
            for(auto it: graf[nod])
                if(d[it.first] > cost + it.second)
                    coada.push(mp(cost + it.second, it.first));
        }
    }
}

int main()
{
    FASTIO;
    fin >> n >> m;
    for1(i, m)
    {
        int a, b, c;
        fin >> a >> b >> c;
        graf[a].pb({b, c});
        graf[b].pb({a, c});
    }
    for1(i, n) d[i] = INFINT;

    coada.push({0, 1});
    dijkstra();
    for(int i = 2; i <= n; i++)
        fout << (d[i] == INFINT ? 0 : d[i]) << ' ';



    return 0;
}