Cod sursa(job #3271623)

Utilizator pascarualexPascaru Alexandru pascarualex Data 26 ianuarie 2025 18:40:20
Problema Floyd-Warshall/Roy-Floyd Scor 50
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.23 kb
#include<bits/stdc++.h>

using namespace std;

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

//vector<vector<pair<int,int>>> adj(250005);
vector<vector<int>> mat(105, vector<int>(105,INT_MAX));
vector<vector<int>> pred(105, vector<int>(105,0));

// vector<int> vis(50005, 0);
// vector<int> dist(50005, INT_MAX);
// vector<int> p(50005, -1);
// queue<int> q;
// int neg;


int n, m;

// int bellman_ford() {
//
//     fin >> n >> m;
//
//     for (int i = 0 ; i < m ; i++) {
//         int x,y,c;
//         fin >> x >> y >> c;
//         adj[x].push_back({y,c});
//     }
//
//     q.push(1);
//     dist[1] = 0;
//     vis[1] = 1;
//     vector<int> negativ (50005, 0);
//
//     while (!q.empty()) {
//         auto from = q.front();
//         q.pop();
//         vis[from] = 0;
//         for (auto [to,w] : adj[from]) {
//             if (dist[from] + w < dist[to]) {
//                 dist[to] = dist[from] + w;
//                 p[to] = from;
//                 if (!vis[to]) {
//                     q.push(to);
//                     vis[to] = 1;
//                 }
//                 negativ[to] = negativ[from] + 1;
//                 if (negativ[to] > n) {
//                     neg = to;
//                     return false;
//                 }
//             }
//         }
//     }
//     return true;
// }
//
// void afisare_ciclu() {
//     stack<int> s;
//     int start = neg;
//     int end = neg;
//     while (p[end] != start) {
//         s.push(end);
//         end = p[end];
//     }
//     s.push(end);
//
//     while (!s.empty()) {
//         fout << s.top() << " ";
//         s.pop();
//     }
// }


void FloydWarshall()
{
    fin >> n;
    for (int i = 1 ; i <= n ; i ++) {
        for (int j = 1; j <= n ; j++) {
            int x;
            fin >> x;
            if (i != j) {
                mat[i][j] = x;
                pred[i][j] = i;
            }
        }
    }

    for (int k = 1; k <= n; k++)
    {
        for (int i = 1; i <= n; i++)
        {
            for (int j = 1; j <= n; j++)
            {
                if (mat[i][k] < INT_MAX && mat[k][j] < INT_MAX)
                {
                    if (mat[i][j] > mat[i][k] + mat[k][j])
                    {
                        mat[i][j] = mat[i][k] + mat[k][j];
                        pred[i][j] = pred[k][j];
                    }
                }
            }
        }
    }

    for (int i = 1; i <= n; i++)
    {
        for (int j = 1; j <= n; j++)
        {
            if (mat[i][j] == INT_MAX)
                mat[i][j] = 0;
            if (i == j)
                mat[i][j] = 0;
        }
    }
}

int main() {
    // if (bellman_ford()) {
    //     for (int i = 2; i <= n; i++) {
    //         if (dist[i] == INT_MAX) {
    //             fout << 0 << " ";
    //         }else {
    //             fout << dist[i] << " ";
    //         }
    //     }
    // }else {
    //     fout << "Ciclu negativ!" << '\n';
    //     afisare_ciclu();
    // }
    FloydWarshall();
    for (int i = 1 ; i <= n ; i++) {
        for (int j = 1; j <= n ; j++) {
            fout << mat[i][j] << " ";
        }
        fout << '\n';
    }
}