Cod sursa(job #3208663)

Utilizator camelia22Dragoiu Camelia camelia22 Data 29 februarie 2024 11:35:18
Problema Distante Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.63 kb
#include <iostream>
#include <fstream>
#define N 50005
#define M 100005
#define maxi 2000000000

using namespace std;
ifstream f("distante.in");
ofstream g("distante.out");

int k, n, m, s, i, j;
int t[3][2*M], c[M], start[N], cost[N], v[N];

void liste_adiacena(int t[3][2*M], int start[], int m)
{
    int k=0, i, x, y, c;
    for (i = 1; i <= n; i++) start[i] = 0;
    for (i = 1; i <= m; i++)
    {
        f >> x >> y >> c;
        t[0][++k] = y; t[1][k] = start[x]; start[x] = k;
        t[0][++k] = x; t[1][k] = start[y]; start[y] = k;
        t[2][k] = t[2][k-1] = c;
    }
}

void ford (int s, int cost[])
{
    int x, i, k, pi=1, ps=1;
    int viz[N] = {0};
    for (i = 1; i <= n; i++)
        cost[i] = maxi;
    cost[s] = 0; c[pi] = s;
    while (ps <= pi)
    {
        k = c[ps];
        viz[k] = 0;
        x = start[k];
        while (x)
        {
            if (cost[k] + t[2][x] < cost[t[0][x]])
            {
                cost[t[0][x]] = cost[k] + t[2][x];
                if (!viz[t[0][x]])
                    viz[t[0][x]] = 1, c[++pi] = t[0][x];
            }
            x = t[1][x];
        }
        ps++;
    }
}

int verif(int v[], int n, int cost[])
{
    int i;
    for (i = 1; i <= n; i++)
        if (cost[i] != v[i])
          return 0;
    return 1;
}

int main()
{
    f >> k;
    for (i=1; i <= k; i++)
    {
        f >> n >> m >> s;
        for (j = 1; j <= n; j++)
            f >> v[j];

        liste_adiacena(t,start,m);
        ford(s,cost);

        if (verif(v,n,cost)) g << "DA";
        else g << "NU";
        g << '\n';
    }
    return 0;
}