Cod sursa(job #3376031)

Utilizator stefan1010Stefan Bogdan stefan1010 Data 15 noiembrie 2026 14:29:57
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.94 kb
#include <fstream>
#include <vector>
#include <queue>
const int NMAX=30000;
const int INF=1e9;
using namespace std;
ifstream fin("sate.in");
ofstream fout("sate.out");

int dist[NMAX+1];
vector<pair<int,int>>l_ad[NMAX+1];
int x,y;
int n,m;

void bfs(int start)
{
    queue<int>q;
    q.push(start);
    dist[start]=0;
    while(!q.empty())
    {
        int nod=q.front();
        q.pop();
        for(auto p:l_ad[nod])
        {
            if(dist[p.first]==INF)
            {
                dist[p.first]=dist[nod]+p.second;
                q.push(p.first);
            }
        }
    }
}
int main()
{
    fin>>n>>m>>x>>y;
    for(int i=1;i<=m;i++)
    {
        int x,y,d;
        fin>>x>>y>>d;
        dist[x]=dist[y]=INF;
        if(x!=min(x,y))
            swap(x,y);
        l_ad[x].push_back(make_pair(y,d));
        l_ad[y].push_back(make_pair(x,-d));
    }
    bfs(x);
    fout<<dist[y];
    return 0;
}