Cod sursa(job #3221131)

Utilizator Robilika2007Robert Badea Robilika2007 Data 6 aprilie 2024 00:23:18
Problema Sate Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.5 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;

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

#define MAX_N 30001
bool viz[MAX_N], ans;

struct zdrang
{
    int cost, directie;
};

vector<zdrang> graf[MAX_N];

void dfs(int node, int cost, int goal)
{
    cout << node << " " << cost << '\n';

    if(ans != 0) return;
    if(node == goal)
    {
        out << cost;
        ans = cost;
        //cout << y;
        return;
    }

    for(int i = 0; i < graf[node].size(); ++i)
    {
        zdrang next = graf[node][i];
        //cout << next << " ";
        if(!viz[next.directie])
        {
            viz[next.directie] = 1;
            dfs(next.directie, next.cost + cost, goal);
        }
    }
}

int main()
{
    int n, m, x, a, b, d, y;

    in >> n >> m >> x >> y;
    //cout << n << " " << m << " " << x << " " << y << '\n';
    for(int i = 0; i < m; ++i)
    {
        in >> a >> b >> d;

        zdrang nr1, nr2;
        nr1.cost = d;
        nr2.cost = -d;
        nr1.directie = b;
        nr2.directie = a;

        graf[a].push_back(nr1);
        graf[b].push_back(nr2);
    }

    for(int i = 1; i <= n; ++i)
    {
        cout << i << " -> \n";
        for(auto zdr : graf[i])
        {
            cout << zdr.directie << " " << zdr.cost << '\n';
        }
        //cout << '\n';
    }

    cout << '\n';

    viz[x] = 1;
    dfs(x, 0, y);

    //out << ans;
    return 0;
}