Cod sursa(job #789106)

Utilizator JohnPeterJohn Peter JohnPeter Data 16 septembrie 2012 20:32:32
Problema Floyd-Warshall/Roy-Floyd Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.9 kb
#include<cstdio>
#include<cassert>
#include<algorithm>

using namespace std;

int verts, graph[105][105], ans[105][105];

void read(){
  assert(freopen("royfloyd.in", "r", stdin));

  scanf("%d", &verts);

  for(int i = 1; i <= verts; ++i)
    for(int j = 1; j <= verts; ++j){
      scanf("%d", &graph[i][j]);

      if(graph[i][j] == 0)
        graph[i][j] = 1000000;

      ans[i][j] = graph[i][j];
    }

}

void solve(){
  for(int i = 1; i <= verts; ++i)
    for(int j = 1; j <= verts; ++j)
      for(int k = 1; k <= verts; ++k)
        ans[j][k] = min(ans[j][k], ans[j][i] + ans[i][k]);
}

void write(){
  assert(freopen("royfloyd.out", "w", stdout));

  for(int i = 1; i <= verts; ++i){
    for(int j = 1; j <= verts; ++j)
      if(ans[i][j] < 1000000 && i != j)
        printf("%d ", ans[i][j]);
      else
        printf("0 ");
    printf("\n");
  }

}

int main(){
  read();
  solve();
  write();
  return 0;
}