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
#include<bits/stdc++.h>
#define inf 1000000009
#define linf (1ll<<60)
#define lim 2009
#define mk make_pair
#define sc second
#define ff first

using namespace std;

struct ver{
	bool safe;
	long long dis_up;
	long long dis_down;
};

char s[lim];
int n,m,k;
ver t[lim][lim];
priority_queue<pair<int,pair<int,int> > > q;

int main()
{
	scanf("%d%d%d", &n, &m, &k);
	t[0][0].safe = false;
	t[0][0].dis_down = linf;
	t[0][0].dis_up = linf;
	for(int i = 0;i<=m+1;++i)
	{
		t[n+1][i] = t[0][i] = t[0][0];
	}
	for(int i = 1;i<=n;++i)
	{
		t[i][0] = t[i][m+1] = t[0][0];
		scanf("%s", s);
		for(int j = 1;j<=m;++j)
		{
			if(s[j-1] == '.')
			{
				t[i][j].safe = true;
			}
			else
			{
				t[i][j].safe = false;
			}
			t[i][j].dis_up = linf;
			t[i][j].dis_down = linf;
		}
	}
	t[1][1].dis_up = 0;
	t[1][1].dis_down = 0;
	q.push(mk(0,mk(1,1)));
	while(!q.empty())
	{
		pair<int,pair<int,int> > pom = q.top();
		q.pop();
		int i = pom.sc.ff;
		int j = pom.sc.sc;
		if(-pom.ff<=t[i][j].dis_up+t[i][j].dis_down)
		{
			int odl = t[i][j].dis_up+t[i][j].dis_down+1;
			if(t[i+1][j].safe)
			{
				if(t[i+1][j].dis_up+t[i+1][j].dis_down > odl)
				{
					t[i+1][j].dis_down = t[i][j].dis_down;
					t[i+1][j].dis_up =  t[i][j].dis_up+1;
					q.push(mk(-odl,mk(i+1,j)));
				}
			}
			if(t[i][j+1].safe)
			{
				if(t[i][j+1].dis_up+t[i][j+1].dis_down > odl)
				{
					t[i][j+1].dis_down = t[i][j].dis_down;
					t[i][j+1].dis_up =  t[i][j].dis_up+1;
					q.push(mk(-odl,mk(i,j+1)));
				}
			}
			if(t[i-1][j].safe)
			{
				if(t[i-1][j].dis_up+t[i-1][j].dis_down > odl)
				{
					t[i-1][j].dis_down = t[i][j].dis_down+1;
					t[i-1][j].dis_up =  t[i][j].dis_up;
					q.push(mk(-odl,mk(i-1,j)));
				}
			}
			if(t[i][j-1].safe)
			{
				if(t[i][j-1].dis_up+t[i][j-1].dis_down > odl)
				{
					t[i][j-1].dis_down = t[i][j].dis_down+1;
					t[i][j-1].dis_up =  t[i][j].dis_up;
					q.push(mk(-odl,mk(i,j-1)));
				}
			}
		}
	}
	long long sm = linf;
	int wyn = 0;
	for(int i = 0;i<k;++i)
	{
		int a,b;
		scanf("%d%d", &a, &b);
		if(t[n][m].dis_up*a+t[n][m].dis_down*b<sm)
		{
			sm = t[n][m].dis_up*a+t[n][m].dis_down*b;
			wyn = 1;
		}
		else
		if(t[n][m].dis_up*a+t[n][m].dis_down*b == sm)
		{
			++wyn;
		}
	}
	printf("%lld %d\n", sm, wyn);
}