L3-018 森森美图
#include<bits/stdc++.h>
using namespace std;
typedef pair<double,int> P;
#define FI first
#define SE second
int dr[2][4] = {0,0,1,-1,-1,1,0,0};
int dr2[2][4] = {1,1,-1,-1,-1,1,-1,1};
typedef long long LL;
struct Point
{
int x,y;
Point(int xx = 0,int yy = 0):x(xx),y(yy){}
bool operator == (const Point &a){
return a.x == x && a.y == y;
}
};
const int maxn = 100+10,maxm = maxn*maxn;
int a[maxn][maxn];
double dis[maxn][maxn];
bool done[maxn][maxn];
Point S,T;
int N,M;
int DIR;
int Multiply(Point &a,Point &b,Point & c){
int x1 = b.x-a.x;
int y1 = b.y-a.y;
int x2 = c.x-a.x;
int y2 = c.y-a.y;
return x1*y2-x2*y1;
}
bool judge(int x,int y){
if( x < 0 ||x >= N || y < 0 || y >= M || done[x][y]) return false;
Point c(x,y);
if(c == S ||c == T) return true;
if(DIR == 0 && Multiply(S,T,c) < 0) return true;
if(DIR == 1 && Multiply(S,T,c) > 0) return true;
return false;
}
void Dij(){
for(int i = 0;i < N; ++i)
for(int j = 0;j < M; ++j)
dis[i][j] = 0x3f3f3f3f,done[i][j] = 0;
priority_queue<P> Q;
Q.push(P(0,S.x*M+S.y));
dis[S.x][S.y] = a[S.x][S.y];
while(!Q.empty()){
P p = Q.top(); Q.pop();
int x = p.SE/M,y = p.SE%M;
if(done[x][y]) continue;
done[x][y] = true;
for(int i = 0;i < 4; ++i){
int xx = x + dr[0][i];
int yy = y + dr[1][i];
if(!judge(xx,yy)) continue;
if(dis[xx][yy] > a[xx][yy] + dis[x][y]) {
dis[xx][yy] = a[xx][yy] + dis[x][y];
Q.push(P(-dis[xx][yy],xx*M+yy));
}
}
for(int i = 0;i < 4; ++i){
int xx = x + dr2[0][i];
int yy = y + dr2[1][i];
if(!judge(xx,yy)) continue;
double e = (sqrt(2)-1)*(a[xx][yy]+a[x][y]);
if(dis[xx][yy] > a[xx][yy] + e + dis[x][y])
{
dis[xx][yy] = a[xx][yy] + e + dis[x][y];
Q.push(P(-dis[xx][yy],xx*M+yy));
}
}
}
}
int main(void){
cin>>N>>M;
for(int i = 0;i < N; ++i){
for(int j = 0;j < M; ++j){
cin>>a[i][j];
}
}
int x1,y1,x2,y2;
cin>>y1>>x1>>y2>>x2;
S = Point(x1,y1),T = Point(x2,y2);
double ans = 0;
DIR = 1,Dij(),ans += dis[x2][y2];
DIR = 0,Dij(),ans += dis[x2][y2];
ans -= a[x2][y2];
ans -= a[x1][y1];
printf("%.2f\n",ans+1e-6);
return 0;
}