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
#include<bits/stdc++.h>
using namespace std;
const int R=2000;
#define fr first
#define sc second
using pib=pair<int,bool>;
using pii=pair<int,int>;
using ll=long long;
using pll=pair<ll,ll>;
pii dist[R*R+6];
vector<pib>graf[R*R+6];
vector<pii>opcje;
void bfs(int id)
{
    queue<int>kol;
    kol.push(1);
    bool vis[R*R+6];
    vis[1]=1;
    while(!kol.empty())
    {
        int u=kol.front();
        kol.pop();
        for(auto it:graf[u])
        {
            int u2=it.fr,k=it.sc;
            if(vis[u2]==0)
            {
                vis[u2]=1;
                dist[u2]=dist[u];
                if(k==0)dist[u2].sc++;
                else dist[u2].fr++;
                if(u2==id)return;
                kol.push(u2);
            }
        }
    }
}
int tab[R+6][R+6];
int main()
{
    ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    int n,m,k;cin>>n>>m>>k;
    int id=0;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=m;j++)
        {
            char c;cin>>c;
            if(c=='X')tab[i][j]=-1;
            else
            {
                id++;
                tab[i][j]=id;
                int v=tab[i][j-1];
                if((v!=-1)&&(j-1>0))
                {
                    graf[id].push_back({v,0});
                    graf[v].push_back({id,1});
                }
                v=tab[i-1][j];
                if((v!=-1)&&(i-1>0))
                {
                    graf[id].push_back({v,0});
                    graf[v].push_back({id,1});
                }
            }
        }
    }
    bfs(id);pll s=dist[id];
    ll ans=LONG_MAX,il=0;
    for(int i=1;i<=k;i++)
    {
        ll a,b;cin>>a>>b;
        ll temp=a*s.fr+b*s.sc;
        if(temp<ans)
        {
            ans=temp;
            il=1;
        }
        else if(temp==ans)il++;
    }
    cout<<ans<<" "<<il;
}