#include<iostream> #include<vector> #include<queue> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); long long n, m, k, a, b; pair<long long, long long> t; cin >> n >> m >> k; vector<vector<char>> Mapa(n, vector<char> (m)); for(long long i=0; i<n; i++) for(long long j=0; j<m; j++) cin >> Mapa[i][j]; priority_queue<pair<long long,long long>> Q; vector<long long> Wynik(k); vector<vector<long long>> D(n, vector<long long> (m,1000000000000000000)); D[0][0]=0; t.first = 0; t.second=0; cin >> a >> b; Q.push(t); while(!Q.empty()) { t=Q.top(); Q.pop(); long long x=t.second/2001; long long y=t.second%2001; long long d=t.first*-1; if(x==n-1&&y==m-1) { Wynik[0]=D[x][y]; break; } if(d!=D[x][y]) continue; if(x-1>=0) if(D[x-1][y]>d+b&&Mapa[x-1][y]=='.') { D[x-1][y]=d+b; t.first=-1*D[x-1][y]; t.second=2001*(x-1)+y; Q.push(t); } if(x+1<n) if(D[x+1][y]>d+a&&Mapa[x+1][y]=='.') { D[x+1][y]=d+a; t.first=-1*D[x+1][y]; t.second=2001*(x+1)+y; Q.push(t); } if(y-1>=0) if(D[x][y-1]>d+b&&Mapa[x][y-1]=='.') { D[x][y-1]=d+b; t.first=-1*D[x][y-1]; t.second=2001*x+y-1; Q.push(t); } if(y+1<m) if(D[x][y+1]>d+a&&Mapa[x][y+1]=='.') { D[x][y+1]=d+a; t.first=-1*D[x][y+1]; t.second=2001*x+y+1; Q.push(t); } } long long nadmiar; nadmiar = (n+m-2)*a; nadmiar*=-1; nadmiar+=Wynik[0]; nadmiar/=(a+b); for(long long i=1; i<k; i++) { cin >> a >> b; Wynik[i]=(n+m-2)*a; Wynik[i]+=nadmiar*(a+b); } long long counter=1; long long minn=Wynik[0]; for(long long i=1; i<Wynik.size(); i++) { if(Wynik[i]==minn) counter++; if(Wynik[i]<minn) { minn=Wynik[i]; counter = 1; } } cout << minn << ' ' << counter; return 0; }
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 | #include<iostream> #include<vector> #include<queue> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); long long n, m, k, a, b; pair<long long, long long> t; cin >> n >> m >> k; vector<vector<char>> Mapa(n, vector<char> (m)); for(long long i=0; i<n; i++) for(long long j=0; j<m; j++) cin >> Mapa[i][j]; priority_queue<pair<long long,long long>> Q; vector<long long> Wynik(k); vector<vector<long long>> D(n, vector<long long> (m,1000000000000000000)); D[0][0]=0; t.first = 0; t.second=0; cin >> a >> b; Q.push(t); while(!Q.empty()) { t=Q.top(); Q.pop(); long long x=t.second/2001; long long y=t.second%2001; long long d=t.first*-1; if(x==n-1&&y==m-1) { Wynik[0]=D[x][y]; break; } if(d!=D[x][y]) continue; if(x-1>=0) if(D[x-1][y]>d+b&&Mapa[x-1][y]=='.') { D[x-1][y]=d+b; t.first=-1*D[x-1][y]; t.second=2001*(x-1)+y; Q.push(t); } if(x+1<n) if(D[x+1][y]>d+a&&Mapa[x+1][y]=='.') { D[x+1][y]=d+a; t.first=-1*D[x+1][y]; t.second=2001*(x+1)+y; Q.push(t); } if(y-1>=0) if(D[x][y-1]>d+b&&Mapa[x][y-1]=='.') { D[x][y-1]=d+b; t.first=-1*D[x][y-1]; t.second=2001*x+y-1; Q.push(t); } if(y+1<m) if(D[x][y+1]>d+a&&Mapa[x][y+1]=='.') { D[x][y+1]=d+a; t.first=-1*D[x][y+1]; t.second=2001*x+y+1; Q.push(t); } } long long nadmiar; nadmiar = (n+m-2)*a; nadmiar*=-1; nadmiar+=Wynik[0]; nadmiar/=(a+b); for(long long i=1; i<k; i++) { cin >> a >> b; Wynik[i]=(n+m-2)*a; Wynik[i]+=nadmiar*(a+b); } long long counter=1; long long minn=Wynik[0]; for(long long i=1; i<Wynik.size(); i++) { if(Wynik[i]==minn) counter++; if(Wynik[i]<minn) { minn=Wynik[i]; counter = 1; } } cout << minn << ' ' << counter; return 0; } |