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
#include<bits/stdc++.h>
using namespace std;
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    long long n,m,k;
    cin>>n>>m>>k;
    vector<string>plansza;
    vector<vector<pair<long long,long long>> >d(n,vector<pair<long long,long long>>(m,make_pair(LLONG_MAX,LLONG_MAX)));
    for(long long i=0;i<n;i++){
        string s;
        cin>>s;
        plansza.push_back(s);
    }
    queue<pair<long long,long long>>q;
    q.push(make_pair(0,0));
    d[0][0].first=0;
    d[0][0].second=0;
    auto go=[&](long long i, long long j, long long dist1, long long dist2){
         if (plansza[i][j]=='X'){return;}
         else{
            if(d[i][j].first==LLONG_MAX){
                d[i][j].first=dist1;// LONG LONGI
                d[i][j].second=dist2;
                q.push(make_pair(i,j));
            }
         }
    };
   while(!q.empty()){
        long long i=q.front().first;
        long long j=q.front().second;
        q.pop();
        if(i+1<n){go(i+1,j,d[i][j].first+1,d[i][j].second);}
        if(i-1>=0){go(i-1,j,d[i][j].first,d[i][j].second+1);}
        if(j+1<m){go(i,j+1,d[i][j].first+1,d[i][j].second);}
        if(j-1>=0){go(i,j-1,d[i][j].first,d[i][j].second+1);}
    }
    long long upstream=d[n-1][m-1].first;
    long long downstream=d[n-1][m-1].second;
    long long maks=LLONG_MAX;
    long long liczba=1;
    for(long long i=0;i<k;i++){
        long long x1,x2;
        cin>>x1>>x2;
        long long wynik=x1*upstream+x2*downstream;
        if(wynik<maks){
            liczba=1;
            maks=wynik;
        }
        else if(wynik==maks){
            liczba++;
        }
    }
    cout<<maks<<" "<<liczba;
}