Cod sursa(job #1470030)

Utilizator Burbon13Burbon13 Burbon13 Data 10 august 2015 11:39:53
Problema Floyd-Warshall/Roy-Floyd Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.41 kb
#include <cstdio>
#include <cstring>
using namespace std;

const int nmx = 52;

int n,m;
int mat[nmx][nmx], aux[nmx][nmx];
bool g[nmx][nmx];

void citire() {
    static int nod1, nod2;
    memset(g, 0, sizeof(g));
    scanf("%d %d", &n, &m);
    for(int i = 1; i <= m; ++i) {
        scanf("%d %d", &nod1, &nod2);
        g[nod1][nod2] = g[nod2][nod1] = 1;
    }
    for(int i = 1; i <= n; ++i)
        for(int j = 1; j <= n; ++j)
            scanf("%d", &mat[i][j]);
}

void creare_mat(){
    memset(aux, 0, sizeof(aux));
    for(int i = 1; i <= n; ++i)
        for(int j = i + 1; j <= n; ++j)
           if(g[i][j])
               aux[i][j] = aux[j][i] = mat[i][j];

    for(int k = 1; k <= n; ++k)
        for(int i = 1; i <= n; ++i)
            for(int j = 1; j <= n; ++j)
                if(i != j && aux[i][k] && aux[k][j] && (not aux[i][j] || aux[i][j] > aux[i][k] + aux[k][j]))
                    aux[i][j] = aux[i][k] + aux[k][j];
}

bool verif() {
    for(int i = 1; i <= n; ++i)
        for(int j = 1; j <= n; ++j)
            if(mat[i][j] != aux[i][j])
                return 0;
    return 1;
}

int main() {
    freopen("rfinv.in", "r", stdin);
    freopen("rfinv.out", "w", stdout);

    int nr_teste;
    scanf("%d", &nr_teste);

    while(nr_teste--) {
        citire();
        creare_mat();
        printf("%s\n", verif() ? "DA" : "NU");
    }

    return 0;
}