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
#include<iostream>
#include<queue>

using namespace std;

int odl[2020][2020];


int main(){
    int n,m,l;
    cin >> n >> m >> l;
    string a;
    for(int i=0;i<n;i++){
        cin >> a;
        for(int j=0;j<m;j++){
            if(a[j]=='.'){
                odl[i][j]=-1;
            }
        }
    }
    queue<pair<int,int>> kol;
    kol.push({0,0});
    odl[0][0]=0;
    pair<int,int> ws;
    while(odl[n-1][m-1]==-1){
        ws=kol.front();
        kol.pop();
        if(ws.first>0 && odl[ws.first-1][ws.second]==-1){
            odl[ws.first-1][ws.second]=odl[ws.first][ws.second]+1;
            kol.push({ws.first-1,ws.second});
        }
        if(ws.second>0 && odl[ws.first][ws.second-1]==-1){
            odl[ws.first][ws.second-1]=odl[ws.first][ws.second]+1;
            kol.push({ws.first,ws.second-1});
        }
        if(ws.first+1<n && odl[ws.first+1][ws.second]==-1){
            odl[ws.first+1][ws.second]=odl[ws.first][ws.second]+1;
            kol.push({ws.first+1,ws.second});
        }
        if(ws.second+1<m && odl[ws.first][ws.second+1]==-1){
            odl[ws.first][ws.second+1]=odl[ws.first][ws.second]+1;
            kol.push({ws.first,ws.second+1});
        }
    }
    long long s=(odl[n-1][m-1]-(n-1+m-1))/2;
    long long f=m-1+n-1+s;
    long long mi=1000000000000000000,nmi=0;
    int fc,sc;
    for(int i=0;i<l;i++){
        cin >> fc >> sc;
        if(fc*f+sc*s<mi){
            mi=fc*f+sc*s;
            nmi=1;
        }
        else{
            if(fc*f+sc*s==mi){
                nmi++;
            }
        }
    }
    cout << mi << " " << nmi;
}