#include<bits/stdc++.h> #define ff first #define ss second #define mp make_pair #define pb push_back #define gcu getchar_unlocked #define INF 1000000000 #define ok(_a0,_a1) ((_a0>=1)&&(_a0<=n)&&(_a1>=1)&&(_a1<=m)&&(!t[_a0][_a1])) using namespace std; typedef long long ll; typedef pair<int,int> pp; int n,m,o; ll w=1000000000000000000; int wcount=0; int main() { scanf("%d%d%d",&n,&m,&o); vector<vector<int>>cost(n+2,vector<int>(m+2,INF)); vector<vector<int>>t(n+2,vector<int>(m+2,0)); for(int i=1;i<=n;++i) { char c=gcu(); while((c!='X')&&(c!='.')) c=gcu(); for(int j=1;j<=m;++j) { t[i][j]=(c=='X'); c=gcu(); } } deque<pp> dq; cost[1][1]=0; dq.push_front(mp(1,1)); while(!dq.empty()) { pp di=dq.front(); dq.pop_front(); if(cost[di.ff][di.ss]<cost[di.ff-1][di.ss]) { if(ok(di.ff-1,di.ss)) { cost[di.ff-1][di.ss]=cost[di.ff][di.ss]; dq.push_front(mp(di.ff-1,di.ss)); } } if(cost[di.ff][di.ss]<cost[di.ff][di.ss-1]) { if(ok(di.ff,di.ss-1)) { cost[di.ff][di.ss-1]=cost[di.ff][di.ss]; dq.push_front(mp(di.ff,di.ss-1)); } } if(cost[di.ff][di.ss]+1<cost[di.ff+1][di.ss]) { if(ok(di.ff+1,di.ss)) { cost[di.ff+1][di.ss]=cost[di.ff][di.ss]+1; dq.push_back(mp(di.ff+1,di.ss)); } } if(cost[di.ff][di.ss]+1<cost[di.ff][di.ss+1]) { if(ok(di.ff,di.ss+1)) { cost[di.ff][di.ss+1]=cost[di.ff][di.ss]+1; dq.push_back(mp(di.ff,di.ss+1)); } } } ll w1=cost[n][m],w2=cost[n][m]-n-m+2; for(int i=0;i<o;++i) { ll p,q; scanf("%lld%lld",&p,&q); if(p*w1+q*w2<w) { w=p*w1+q*w2; wcount=0; } if(p*w1+q*w2==w) ++wcount; } printf("%lld %lld\n",w,wcount); }
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 | #include<bits/stdc++.h> #define ff first #define ss second #define mp make_pair #define pb push_back #define gcu getchar_unlocked #define INF 1000000000 #define ok(_a0,_a1) ((_a0>=1)&&(_a0<=n)&&(_a1>=1)&&(_a1<=m)&&(!t[_a0][_a1])) using namespace std; typedef long long ll; typedef pair<int,int> pp; int n,m,o; ll w=1000000000000000000; int wcount=0; int main() { scanf("%d%d%d",&n,&m,&o); vector<vector<int>>cost(n+2,vector<int>(m+2,INF)); vector<vector<int>>t(n+2,vector<int>(m+2,0)); for(int i=1;i<=n;++i) { char c=gcu(); while((c!='X')&&(c!='.')) c=gcu(); for(int j=1;j<=m;++j) { t[i][j]=(c=='X'); c=gcu(); } } deque<pp> dq; cost[1][1]=0; dq.push_front(mp(1,1)); while(!dq.empty()) { pp di=dq.front(); dq.pop_front(); if(cost[di.ff][di.ss]<cost[di.ff-1][di.ss]) { if(ok(di.ff-1,di.ss)) { cost[di.ff-1][di.ss]=cost[di.ff][di.ss]; dq.push_front(mp(di.ff-1,di.ss)); } } if(cost[di.ff][di.ss]<cost[di.ff][di.ss-1]) { if(ok(di.ff,di.ss-1)) { cost[di.ff][di.ss-1]=cost[di.ff][di.ss]; dq.push_front(mp(di.ff,di.ss-1)); } } if(cost[di.ff][di.ss]+1<cost[di.ff+1][di.ss]) { if(ok(di.ff+1,di.ss)) { cost[di.ff+1][di.ss]=cost[di.ff][di.ss]+1; dq.push_back(mp(di.ff+1,di.ss)); } } if(cost[di.ff][di.ss]+1<cost[di.ff][di.ss+1]) { if(ok(di.ff,di.ss+1)) { cost[di.ff][di.ss+1]=cost[di.ff][di.ss]+1; dq.push_back(mp(di.ff,di.ss+1)); } } } ll w1=cost[n][m],w2=cost[n][m]-n-m+2; for(int i=0;i<o;++i) { ll p,q; scanf("%lld%lld",&p,&q); if(p*w1+q*w2<w) { w=p*w1+q*w2; wcount=0; } if(p*w1+q*w2==w) ++wcount; } printf("%lld %lld\n",w,wcount); } |