#include <bits/stdc++.h>
using namespace std;
long long R=1e18,wyn,dod,uje;
vector<vector<pair<long long, long long> > > V;
string S[2001][2001];
long long tab[4000007],n,m,k;
char c;
void djk(long long a)
{
priority_queue<pair<long long, long long> > kolejka;
kolejka.push(make_pair(0,a));
while(!kolejka.empty())
{
pair<long long ,long long> x = kolejka.top();
kolejka.pop();
for(auto it: V[x.second])
{
if(tab[it.first]>tab[x.second]+it.second)
{
tab[it.first]=tab[x.second]+it.second;
kolejka.push(make_pair(-tab[it.first], it.first));
}
}
}
}
int main()
{
std::ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>m>>k;
V.resize((n*m)+1);
for(int i=0;i<n;i++)
{
for(int j=1;j<=m;j++)
{
cin>>c;
S[i][j]=c;
tab[m*i+j]=R;
}
}
for(int i=0;i<n;i++)
{
for(int j=1;j<=m;j++)
{
if(S[i][j]==".")
{
if(S[i+1][j]==".")
{
V[i*m+j].push_back(make_pair((i+1)*m+j,1));
V[(i+1)*m+j].push_back(make_pair((i)*m+j,1e9));
}
if(S[i][j+1]==".")
{
V[i*m+j].push_back(make_pair((i*m+j+1),1));
V[i*m+j+1].push_back(make_pair((i*m+j),1e9));
}
}
}
}
tab[1]=0;
djk(1);
wyn=tab[n*m];
dod=wyn%1000000000;
uje=wyn/1000000000;
long long mini=1e18,a,b;
long long ile=0;
for(int i=0;i<k;i++)
{
cin>>a>>b;
wyn=dod*a+uje*b;
if(wyn<mini)
{
mini=wyn;
ile=1;
}
else
if(wyn==mini)
{
ile++;
}
}
cout<<mini<<" "<<ile;
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 | #include <bits/stdc++.h> using namespace std; long long R=1e18,wyn,dod,uje; vector<vector<pair<long long, long long> > > V; string S[2001][2001]; long long tab[4000007],n,m,k; char c; void djk(long long a) { priority_queue<pair<long long, long long> > kolejka; kolejka.push(make_pair(0,a)); while(!kolejka.empty()) { pair<long long ,long long> x = kolejka.top(); kolejka.pop(); for(auto it: V[x.second]) { if(tab[it.first]>tab[x.second]+it.second) { tab[it.first]=tab[x.second]+it.second; kolejka.push(make_pair(-tab[it.first], it.first)); } } } } int main() { std::ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin>>n>>m>>k; V.resize((n*m)+1); for(int i=0;i<n;i++) { for(int j=1;j<=m;j++) { cin>>c; S[i][j]=c; tab[m*i+j]=R; } } for(int i=0;i<n;i++) { for(int j=1;j<=m;j++) { if(S[i][j]==".") { if(S[i+1][j]==".") { V[i*m+j].push_back(make_pair((i+1)*m+j,1)); V[(i+1)*m+j].push_back(make_pair((i)*m+j,1e9)); } if(S[i][j+1]==".") { V[i*m+j].push_back(make_pair((i*m+j+1),1)); V[i*m+j+1].push_back(make_pair((i*m+j),1e9)); } } } } tab[1]=0; djk(1); wyn=tab[n*m]; dod=wyn%1000000000; uje=wyn/1000000000; long long mini=1e18,a,b; long long ile=0; for(int i=0;i<k;i++) { cin>>a>>b; wyn=dod*a+uje*b; if(wyn<mini) { mini=wyn; ile=1; } else if(wyn==mini) { ile++; } } cout<<mini<<" "<<ile; return 0; } |
English