Cod sursa(job #979568)

Utilizator manutrutaEmanuel Truta manutruta Data 1 august 2013 23:18:05
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.57 kb
# include <cstring>
# include <iostream>
# include <fstream>
# include <vector>
# include <queue>
using namespace std;

# define MAXN 102
# define INF 0x3f3f3f3f
# define cout g

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

int n;
vector<pair<int, int> > G[MAXN];
queue<int> coada;
int dist[MAXN];

void bfs(int nod)
{
    memset(dist, 0x3f, sizeof(dist));
    dist[nod] = 0;
    coada.push(nod);
    while (!coada.empty()){
        int nd = coada.front();
        coada.pop();

        /*for (int i = 0; i < n; i++) {
            cout << dist[i] << ' ';
        }
        cout << endl;*/

        for (int i = 0; i < n; i++) {
            //cout << G[nd][i].second << ' ';
            if (dist[G[nd][i].first] > dist[nd] + G[nd][i].second) {
                dist[G[nd][i].first] = dist[nd] + G[nd][i].second;
                coada.push(G[nd][i].first);
            }
        }
        //cout << endl;
    }

    for (int i = 0; i < n; i++) {
        if (dist[G[nod][i].first] == INF) {
            cout << -1 << ' ';
        } else
            cout << dist[G[nod][i].first] << ' ';
    }
    cout << endl;
}

int main()
{
    f >> n;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            int x;
            f >> x;
            if (x != 0) {
                G[i].push_back(make_pair(j, x));
            } else {
                G[i].push_back(make_pair(j, INF));
            }
        }
        G[i][i].second = 0;
    }

    for (int i = 0; i < n; i++) {
        bfs(i);
    }

    return 0;
}