Cod sursa(job #2553369)

Utilizator LeVladzCiuperceanu Vlad LeVladz Data 21 februarie 2020 21:42:27
Problema Sate Scor 80
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.07 kb
#include <fstream>
#include <vector>

using namespace std;

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

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

void dfs(int nod)
{
    if (!ok)
    {
        v[nod] = 1;
        if (nod == y)
        {
            sol = s;
            ok = 1;
        }
        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;
    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;
}