Cod sursa(job #1044448)

Utilizator xoSauceSergiu Ferentz xoSauce Data 29 noiembrie 2013 21:38:52
Problema Floyd-Warshall/Roy-Floyd Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.79 kb
#include <iostream>
#include <cstdio>
#define inf 0x3f3f3f3f
using namespace std;

int graph[101][101];
int n;


void print(){

  for(int i = 0; i < n; i++){
    for(int j =0; j<n;j++)
      printf("%d ",graph[i][j]);
    printf("\n");
  }
}
void scan(){

  scanf("%d",&n);
  for(int i = 0 ; i < n; i++){
   for(int j = 0; j<n; j++)
   {scanf("%d", &graph[i][j]);
    if(graph[i][j] == 0 && i!=j)
     graph[i][j] = inf;
   }
  }

  print();
  cout << endl;
}

void solve(){

  for(int k = 0 ; k < n; k++){

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

	for(int j = 0; j <n; j++){

	  if(graph[i][j] > graph[i][k] + graph[k][j])
	      graph[i][j] = graph[i][k] + graph[k][j];
	}
      }
  }
}

int main(){
  freopen("royfloyd.in","r",stdin);
  freopen("royfloyd.out","w",stdout);
  scan();
  solve();
  print();
  return 0;
}