Cod sursa(job #3175143)

Utilizator TanasucaGeorgeAlexandruTanasucaGeorgeAlexandru TanasucaGeorgeAlexandru Data 25 noiembrie 2023 12:56:40
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.9 kb
#include <bits/stdc++.h>
using namespace std;

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

int n, m, k, st, d;
int dp[100005];
vector <pair<int,int>> a[100005];
priority_queue <pair<int,int>> q;
bitset<100005> v;
int main()
{
    int i, x, y;
    fin >> n >> m >> st >> k;
    for (i = 1; i <= m; i++){
        fin >> x >> y >> d;
        a[x].push_back({d,y});
        a[y].push_back({-d, x});
    }
    q.push({0,st});
    //for (i = 1; i <= n; i++)
     //   dp[i] = 0;
    while (!q.empty()){
        x = q.top().second;
        q.pop();
        if(!v[x]){
            v[x]=1;
            for (auto w : a[x]){
                dp[w.second]=dp[x]+w.first;
                q.push({-dp[w.second],w.second});
            }
        }
    }
    ///for(i=0;i<=n;i++) cout << dp[i] << " ";
    if (dp[k] == 1e9) fout << "-1\n";
    else fout << dp[k] << "\n";

    return 0;
}