Cod sursa(job #3370389)

Utilizator FrozenRoseIancu Madalin FrozenRose Data 25 octombrie 2026 21:50:10
Problema Sate Scor 0
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.89 kb
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

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

int drumuri_stiute(vector<int> v)
{
    int k = 0;
    for( int i : v)
    {
        if (i > 0)
            k++;
    }
    return k;
}

int main()
{
    int n,m,x,y;
    f>>n>>m>>x>>y;

    vector <vector<int>> distanta (n+1 , vector<int> (n+1, -1));

    for (int i = 1; i<=n; i++)
        distanta[i][i] = 0;
    int i,j,D;
    while (m)
    {
        f>>i>>j>>D;
        distanta[i][j] = D;
        distanta [j][i] = D;
        m--;
    }
    while (distanta[x][y] == -1)
    {
        for (i = 1; i<=n ;i++)
        {
            if (drumuri_stiute(distanta[i]) >= 2)
            {
                for (int ii = 1; ii<=n ; ii++)
                {
                    if (ii != i && distanta[i][ii] > 0)
                        for (int jj = ii; jj <= n ; jj++)
                    {

                        if (distanta[i][jj] > 0)
                        {
                            if (ii < i && i< jj)
                            {
                                distanta[ii][jj] = distanta [i][ii] + distanta[i][jj];
                            }
                            if (i < ii && i < jj)
                            {
                                distanta[ii][jj] = distanta[i][jj] - distanta[i][ii];
                            }
                            if (ii < i && jj < i)
                            {
                                distanta[ii][jj] = distanta[i][jj] - distanta[i][ii];
                            }
                        }
                    }

                }
            }

        }
    }

    for(int i = 1 ; i<=n ;i++)
    {
        for(int j = 1; j<= n; j++)
            cout<<distanta[i][j]<<"  ";
        cout<<"\n";
    }
    g<<distanta[x][y];

    return 0;
}