Cod sursa(job #2553364)

Utilizator LeVladzCiuperceanu Vlad LeVladz Data 21 februarie 2020 21:38:30
Problema Sate Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 0.97 kb
#include <fstream>
#include <vector>
#define INF 2000000000

using namespace std;

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

int n,m,i,x,y,x1,y1,cost,sol,s,v[30005];
vector< pair<int, int> > L[30005];

void dfs(int nod)
{
    v[nod] = 1;
    if (nod == y)
        sol = min(sol, s);
    for (int i=0; i<L[nod].size(); i++)
    {
        int vecin = L[nod][i].first; int cost = L[nod][i].second;
        if (!v[vecin])
        {
            if (vecin > nod)
                s += cost;
            else
                s -= cost;
            dfs(vecin);
            if (vecin > nod)
                s -= cost;
            else
                s += cost;
        }
    }
}

int main()
{
    fin >> n >> m >> x >> y; sol = INF;
    for (i=1; i<=m; i++)
    {
        fin >> x1 >> y1 >> cost;
        L[x1].push_back(make_pair(y1, cost));
        L[y1].push_back(make_pair(x1, cost));
    }
    dfs(x);
    fout << sol;
    return 0;
}