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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define st first
#define nd second

bool nieb[2000][2000];//niebezpieczenstwo

ll Dijkstra(ll n, ll m, ll gora, ll dol)
{
    vector<vector<ll>> d;
    d.resize(n);
    for(auto& e:d)
        e.resize(m);
    d[0][0] = 1;//0 oznacza nieskonczonosc
    priority_queue<pair<ll, pair<ll,ll>>> Q;
    Q.push({-d[0][0], {0,0}});

    while(!Q.empty())
    {
        pair<ll,ll> v = Q.top().nd;
        Q.pop();
        if(v.st+1<n && !nieb[v.st+1][v.nd] && (d[v.st+1][v.nd]> d[v.st][v.nd]+gora || d[v.st+1][v.nd]==0 ))
        {
            d[v.st+1][v.nd] = d[v.st][v.nd]+gora;
            Q.push({-d[v.st+1][v.nd],{v.st+1,v.nd}});
        }
        if(v.st-1>=0 && !nieb[v.st-1][v.nd] && (d[v.st-1][v.nd]> d[v.st][v.nd]+dol || d[v.st-1][v.nd]==0))
        {
            d[v.st-1][v.nd] = d[v.st][v.nd]+dol;
            Q.push({-d[v.st-1][v.nd],{v.st-1,v.nd}});
        }
        if(v.nd+1<m && !nieb[v.st][v.nd+1] && (d[v.st][v.nd+1]> d[v.st][v.nd]+gora || d[v.st][v.nd+1]==0))
        {
            d[v.st][v.nd+1] = d[v.st][v.nd]+gora;
            Q.push({-d[v.st][v.nd+1],{v.st,v.nd+1}});
        }
        if(v.nd-1>=0 && !nieb[v.st][v.nd-1] && (d[v.st][v.nd-1]> d[v.st][v.nd]+dol || d[v.st][v.nd-1]==0))
        {
            d[v.st][v.nd-1] = d[v.st][v.nd]+dol;
            Q.push({-d[v.st][v.nd-1],{v.st,v.nd-1}});
        }
    }
    /*for(auto& k:d)
    {
        for(auto& e:k)
            cout<<e<<" ";
        cout<<"\n";
    }*/
    return d[n-1][m-1]-1;
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    ll n, m;
    cin>>n>>m;
    ll k;
    cin>>k;
    for(ll i = 0; i<n; i++)
    {
        for(ll j=0; j<m; j++)
        {
            char z;
            cin>>z;
            if(z=='X')
                nieb[i][j]=true;
        }
    }
    vector<ll> czasy;
    czasy.resize(k);
    for(ll z = 0; z<k; z++)//dla kazdego znajomego
    {
        ll gora, dol;
        cin>>gora>>dol;
        czasy[z]=Dijkstra(n,m,gora,dol);
    }
    ll min = czasy[0];
    for(auto& t:czasy)
    {
        if(t<min)
            min=t;
    }
    ll ilosc=0;
    for(auto& t: czasy)
    {
        if(t==min)
            ilosc++;
    }
    cout<<min<<" "<<ilosc;
}