Cod sursa(job #1164612)

Utilizator CosminRusuCosmin Rusu CosminRusu Data 2 aprilie 2014 10:38:15
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.97 kb
#include <fstream>
#include <iostream>
#include <vector>
#include <bitset>
#include <string.h>
#include <algorithm>
#include <iomanip>
#include <math.h>
#include <time.h>
#include <stdlib.h>
#include <set>
#include <map>
#include <string>
#include <queue>
#include <deque>

using namespace std;

const char infile[] = "royfloyd.in";
const char outfile[] = "royfloyd.out";

ifstream fin(infile);
ofstream fout(outfile);

const int MAXN = 105;
const int oo = 0x3f3f3f3f;

typedef vector<int> Graph[MAXN];
typedef vector<int> :: iterator It;

const inline int min(const int &a, const int &b) { if( a > b ) return b;   return a; }
const inline int max(const int &a, const int &b) { if( a < b ) return b;   return a; }
const inline void Get_min(int &a, const int b)    { if( a > b ) a = b; }
const inline void Get_max(int &a, const int b)    { if( a < b ) a = b; }

int N, A[MAXN][MAXN];

const int lim = (1 << 20);
char buff[lim];
int pos;


inline void get(int &x) {
    x = 0;
    while(!('0' <= buff[pos] && buff[pos] <= '9'))
        if(++ pos == lim) {
            fread(buff, 1, lim, stdin);
            pos = 0;
        }
    while('0' <= buff[pos] && buff[pos] <= '9') {
        x = x * 10 + buff[pos] - '0';
        if(++ pos == lim) {
            fread(buff, 1, lim, stdin);
            pos = 0;
        }
    }
}

int main() {
    freopen(infile, "r", stdin);
    get(N);
    for(int i = 1 ; i <= N ; ++ i)
        for(int j = 1 ; j <= N ; ++ j) {
            get(A[i][j]);
            if(i == j)
                continue;
            if(!A[i][j])
                A[i][j] = oo;
        }
    for(int k = 1 ; k <= N ; ++ k)
        for(int i = 1 ; i <= N ; ++ i)
            for(int j = 1 ; j <= N ; ++ j)
                A[i][j] = min(A[i][j], A[i][k] + A[k][j]);
    for(int i = 1 ; i <= N ; ++ i, fout << '\n')
        for(int j = 1 ; j <= N ; ++ j) {
            if(A[i][j] == oo)
                A[i][j] = 0;
            fout << A[i][j] << ' ';
        }
    fin.close();
    fout.close();
    return 0;
}