Cod sursa(job #2327059)

Utilizator FrequeAlex Iordachescu Freque Data 24 ianuarie 2019 13:01:43
Problema Algoritmul lui Dijkstra Scor 20
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 2.23 kb
#include <bits/stdc++.h>
#define ios ios::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define enter cout << '\n'

using namespace std;

typedef long long ll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef vector <int> vi;
typedef vector <pii> vii;
typedef vector <ll> vl;
typedef vector <pll> vll;
typedef queue <int> qi;
typedef queue <pii> qii;
typedef queue <ll> ql;
typedef queue <pll> qll;

const int INF = 0x3f3f3f3f;
const int MOD = 998244353;
const int EPSILON = 0.0000000001;
//const int NMAX = 2000 + 5;
const int NMAX = 50000 + 5;
const int MMAX = 1e4 + 5;
const int KMAX = 15 + 5;
const int EMAX = (1 << 16);

ifstream fin("dijkstra.in");
ofstream fout("dijkstra.out");

struct edge
{
    int node, cost;

    edge()
    {
        node = cost = 0;
    }

    edge(int _n, int _c)
    {
        node = _n;
        cost = _c;
    }

    bool operator <(const edge &arg) const
    {
        return cost > arg.cost;
    }
};

priority_queue <edge> pq;
vector <edge> graph[NMAX], comp[KMAX];
int n, m, k;
int prieten[KMAX], dist[NMAX];
bool vis[NMAX];

void dijkstra(int start)
{
    memset(dist, INF, sizeof(dist));
    dist[start] = 0;
    pq.push(edge(start, 0));
    while (!pq.empty())
    {
        edge nod = pq.top();
        pq.pop();

        if (!vis[nod.node])
        {
            vis[nod.node] = true;
            for (edge next: graph[nod.node])
                if (dist[next.node] > dist[nod.node] + next.cost)
                {
                    dist[next.node] = dist[nod.node] + next.cost;
                    if (!vis[next.node]) pq.push(edge(next.node, dist[next.node]));
                }
        }
    }
}

void read()
{
    int x, y, c;

    fin >> n >> m;// >> k;
//    for (int i = 1; i <= k; ++i)
  //      cin >> prieten[i];
    for (int i = 1; i <= m; ++i)
    {
        fin >> x >> y >> c;
        graph[x].pb(edge(y, c));
        graph[y].pb(edge(x, c));
    }
}

int main()
{
    read();
    dijkstra(1);
    for (int i = 2; i <= n; ++i)
        if (dist[i] == INF) fout << "0 ";
        else fout << dist[i] << " ";

    return 0;
}