Cod sursa(job #2758295)

Utilizator VanillaSoltan Marian Vanilla Data 9 iunie 2021 18:41:26
Problema Algoritmul lui Dijkstra Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.83 kb
#include <bits/stdc++.h>
using namespace std;
string __fname = "dikstra";
ifstream in (__fname + ".in");
ofstream out (__fname + ".out");
#define cin in
#define cout out
typedef long long int64;
typedef vector<int> vec;
typedef vector<int64> vec64;
#define qr queries();
void solve(int);
void queries(){int n;scanf("%d", &n);for (int i = 1; i <= n; i++) solve(i);}
void setIO(string filename){if(fopen((filename + ".in").c_str(), "r")){freopen((filename + ".in").c_str(), "r", stdin);freopen((filename + ".out").c_str(), "w", stdout);}}
int64 ceildiv(int64 a, int64 b) {return a / b + !!(a % b);}
// // // // // // // // // // // // // // // // // // // // // // 
/*                  TEMPLATE - VANILLA                         */
// // // // // // // // // // // // // // // // // // // // // //
const int maxn = 200200;
const int64 mod = 1000000007;
const double pi = 3.14159265359;
const int ddx[] = {-1, -1, 0, 1, 1, 1, 0, -1};
const int ddy[] = {0, 1, 1, 1, 0, -1, -1, -1};
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
 
void solve(int id){

    return;
}
 
 
int main(){
    // ios_base::sync_with_stdio(0);cin.tie(0);
    int n,m;
    cin >> n >> m;
    vector <vector <pair <int, int> > > v (n + 1);
    vector <int> d (n + 1, 1e8);
    vector <bool> viz (n + 1, false);
    d[1] = 0;
    for (int i = 0; i < m; i++){
        int a,b,c;
        cin >> a >> b >> c;
        v[a].push_back({b, c});
    }
    for (int i = 2; i <= n; i++){
        int node = -1;
        for (int j = 1; j <= n; j++){
            if (!viz[j] && (node == -1 || d[j] < node)) {
                node = j;
            }
        }
        if (d[node] == 1e8) {
            break;
        }
        viz[node] = 1;
        for (auto j: v[node]) {
            d[j.first] = min(d[j.first], d[node] + j.second);
        }
        cout << d[i] << " ";
    }
    return 0;
}