1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <stdio.h>
using namespace std;

struct vertex {
  vertex * next;
  int target;
  bool isUp = false;
};

struct pathResult {
  int up = 0;
  int down = 0;
};

const unsigned long long int MAXINT = 18446744073709551615u;

pathResult *calculate(vertex **graf, unsigned int vertexCount, unsigned long long int timeUp, unsigned long long int timeDown) {
  unsigned int i, j, k, previous[vertexCount];
  unsigned long long int travelTime[vertexCount];
  bool visited[vertexCount];
  vertex *v;

  for (i = 0; i < vertexCount; ++i) {
    travelTime[i] = i == 0 ? 0 : MAXINT;
    visited[i] = false;
    previous[i] = -1;
  }

  for (i = 0; i < vertexCount; ++i) {
    for (j = 0; visited[j]; ++j);
    for (k = j++; j < vertexCount; ++j) {
      if (!visited[j] && (travelTime[j] < travelTime[k])) k = j;
    }

    visited[k] = true;

    for (v = graf[k]; v; v = v->next) {
      unsigned long long int time = v->isUp ? timeUp : timeDown;
      if (!visited[v->target] && (travelTime[v->target] > travelTime[k] + time)) {
        travelTime[v->target] = travelTime[k] + time;
        previous[v->target] = k;
      }
    }
  }

  unsigned int path[vertexCount];
  pathResult *result = new pathResult;
  for (int index = vertexCount - 1; index > 0; index = previous[index]) {
    for (v = graf[previous[index]]; v; v = v->next) {
      if (v->target == index) {
        if (v->isUp) result->up++;
        else result->down++;
        break;
      }
    }
  }

  return result;
}

int main(int argc, char const *argv[]) {
  unsigned int i, j, width, height, count, resultCount = 0, vertexCount = 0;
  unsigned long long int timeUp, timeDown, result = 0;
  char region;
  vertex **graf;
  vertex *v;
  if (!scanf("%d %d %d\n", &height, &width, &count)) return 1;

  int map[height][width];
  for (i = 0; i < height; ++i) {
    for (j = 0; j < width; ++j) {
      if (!scanf(j == (width - 1) ? "%c\n" : "%c", &region)) return 1;
      map[i][j] = region == 'X' ? -1 : vertexCount++;
    }
  }

  graf = new vertex * [vertexCount];

  for (i = 0; i < vertexCount; ++i) {
    graf[i] = NULL;
  }

  for (i = 0; i < height; ++i) {
    for (j = 0; j < width; ++j) {
      if (map[i][j] >= 0) {
        if (i < height - 1 && map[i+1][j] >= 0) {
          v = new vertex;
          v->target = map[i+1][j];
          v->isUp = true;
          v->next = graf[map[i][j]];
          graf[map[i][j]] = v;

          v = new vertex;
          v->target = map[i][j];
          v->isUp = false;
          v->next = graf[map[i+1][j]];
          graf[map[i+1][j]] = v;
        }

        if (j < width - 1 && map[i][j+1] >= 0) {
          v = new vertex;
          v->target = map[i][j+1];
          v->isUp = true;
          v->next = graf[map[i][j]];
          graf[map[i][j]] = v;

          v = new vertex;
          v->target = map[i][j];
          v->isUp = false;
          v->next = graf[map[i][j+1]];
          graf[map[i][j+1]] = v;
        }
      }
    }
  }

  pathResult *resultEq = calculate(graf, vertexCount, 1, 1);
  pathResult *resultGt = calculate(graf, vertexCount, 1, 10);
  pathResult *resultLt = calculate(graf, vertexCount, 10, 1);
  result = MAXINT;
  unsigned long long int temp;

  for (i = 0; i < count; ++i) {
    if (!scanf("%lld %lld", &timeUp, &timeDown)) return 1;
    if (timeUp > timeDown) {
      temp = resultGt->up * timeUp + resultGt->down * timeDown;
    } else if (timeUp < timeDown) {
      temp = resultLt->up * timeUp + resultLt->down * timeDown;
    } else {
      temp = resultEq->up * timeUp + resultEq->down * timeDown;
    }

    if (temp < result) {
      result = temp;
      resultCount = 1;
    } else if (temp == result) {
      ++resultCount;
    }
  }

  printf("%lld %d", result, resultCount);
  return 0;
}