Cod sursa(job #1689709)

Utilizator razvandRazvan Dumitru razvand Data 14 aprilie 2016 15:14:18
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.16 kb
#include <iostream>
#include <vector>
#include <fstream>
#include <queue>
#include <climits>
#define MAX 103

using namespace std;

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

int cost[MAX][MAX];
int dist[MAX][MAX];
int n,v;

void bell(int src) {
    queue<int> que;
    for(int i = 1; i <= n; i++)
        dist[src][i] = INT_MAX/2-1;
    dist[src][src] = 0;
    que.push(src);
    int nod;
    vector<pair<int, int>>::iterator it;
    while(!que.empty()) {
        nod = que.front();
        que.pop();
        for(int i = 1; i <= n; i++) {
            if(cost[nod][i] == 0)
                continue;
            if(dist[src][nod]+cost[nod][i] < dist[src][i]) {
                dist[src][i] = dist[src][nod]+cost[nod][i];
                que.push(i);
            }
        }
    }
}

int main() {

    in >> n;

    for(int i = 1; i <= n; i++)
        for(int j = 1; j <= n; j++)
            in >> cost[i][j];

    for(int i = 1; i <= n; i++)
        bell(i);

    for(int i = 1; i <= n; i++) {
        for(int j = 1; j <= n; j++)
            out << dist[i][j] << " ";
        out << '\n';
    }

    return 0;
}