#ifndef ConMatrixH
#define ConMatrixH

 #include "stdio.h"

 typedef int** CMatrix;

 void InitCMatrix(CMatrix *m, int c){
  int i, j;
  (*m) = calloc(c, sizeof(int));
  for (i=0; i<c; i++)
    (*m)[i] = calloc(c, sizeof(int));

  for (i=0; i<c; i++)
   for (j=0; j<c; j++)
    (*m)[i][j] = -1;
 }


 int ReadTimeComponents(char *s, struct stations *stat, CMatrix m){
  FILE *f;
  int i, id1, id2;
  char str1[256], str2[256];

  if ((f = fopen(s,"rt")) == NULL) return -1;

   while (!feof(f)) {
    fscanf(f, "%s%s%d",  &str1, &str2, &i);
    id1 = GetStationID(&str1, stat);
    id2 = GetStationID(&str2, stat);

     // if (id1<id2)  m[id2][id1] = i; else m[id1][id2] = i;
     m[id2][id1] = i;
     m[id1][id2] = i;
   }

   fclose(f);
  return 1;
 }


 int ReadConComponents(char *s, struct stations *stat, CMatrix m){
  FILE *f;
  int i, id1, id2;
  char str1[256], str2[256];

  if ((f = fopen(s,"rt")) == NULL) return -1;

   while (!feof(f)) {
    fscanf(f, "%s%s%d",  &str1, &str2, &i);
    id1 = GetStationID(&str1, stat);
    id2 = GetStationID(&str2, stat);

     // if (id1<id2)  m[id2][id1] = i; else m[id1][id2] = i;
     m[id2][id1] = 1;
     m[id1][id2] = 1;
   }

   fclose(f);
  return 1;
 }


 
 int GetCompAB(CMatrix m, struct Stations *stat, char *s1, char *s2){
  return m[GetStationID(s1, stat)][GetStationID(s2, stat)];
 }


 void DestCMatrix(CMatrix *m,  int c){
  int i;

   for (i=0; i<c; i++)
     free((*m)[i]);

   free(*m);
 }


 void PrintCMatrix(CMatrix m,  int c){
  int i, j;

   for (i=0; i<c; i++) {
    for (j=0; j<c; j++)

     if (m[i][j]==0)
       printf( " x ");
      else
       printf( "%2.d ", m[i][j]);

    printf("\n");
    }
 }



// 1 - if (-1)-matrix
 char TestCMatrix(CMatrix m, int c, int p){
  int i, j;

   for (j=1; j < c; j++)
    for (i=0; i < j; i++)

     if (i != p)
      if (m[i][j] > 0 && m[j][i] > 0)
        return 0;



  return 1;
 }



#endif