Cod sursa(job #3182843)

Utilizator paull122Paul Ion paull122 Data 9 decembrie 2023 21:12:19
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.09 kb
#include <bits/stdc++.h>

#define ll long long


using namespace std;

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


vector<pair<int,int>> graph[100001];
int dist[100001];
bool vis[100001];

int main()
{
    int n,m,first,last;
    fin >> n >> m >> first >> last;
    if(first > last)
    {
        swap(first,last);
    }

    while(m--)
    {
        int x,y,z;
        fin >> x >> y >> z;
        if(x > y)
        {
            swap(x,y);
        }
        graph[x].push_back({y,z});
        graph[y].push_back({x,-z});
    }
    vis[first]=1;
    queue<int> Q;
    Q.push(first);
    while(!Q.empty())
    {
        int node = Q.front();
        Q.pop();
        for(auto i : graph[node])
        {
            if(!vis[i.first])
            {
                vis[i.first]=1;
                dist[i.first]=dist[node]+i.second;
                Q.push(i.first);
                if(i.first==last)
                {
                    fout << dist[i.first];
                    return 0;
                }
            }
        }
    }

}