Cod sursa(job #2955340)

Utilizator NToniBoSSNicolae Tonitza NToniBoSS Data 16 decembrie 2022 19:13:05
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.8 kb
#include <bits/stdc++.h>
#define LIM 1<<17
/// TONI BO$$ was here
/// #MLC

using namespace std;

vector <pair<int,int>> G[30001];
int f[30001];

void dfs(int x, int dist){
    f[x] = dist;
    for(auto it : G[x])
        if(f[it.first] > f[x] + it.second * (x > it.first ? -1 : 1))
            dfs(it.first, f[x] + it.second * (x > it.first ? -1 : 1));
}

int main()
{
    int n, m, x, y, a, b, c, i;
    freopen("sate.in","r",stdin);
    freopen("sate.out","w",stdout);
    cin >> n >> m >> x >> y;
    for(i = 1; i <= m; i++){
        scanf("%d%d%d", &a, &b, &c);
        G[a].push_back({b, c});
        G[b].push_back({a, c});
    }
    if(x > y)
        swap(x, y);
    for(i = 1; i <= n; i++)
        f[i] = INT_MAX;
    dfs(x, 0);
    printf("%d", f[y]);

    return 0;
}