→题目链接←
跑n*n遍的spfa
每次最后取个max就好
#include<iostream>
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
struct node{
int x,y;
long long len;
};
int tox[4]={-1,0,1,0};
int toy[4]={0,1,0,-1};
int n,A,B;
bool a[33][33];
long long Min[33][33];
long long ans=0;
void shortest_path(int x,int y){
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
Min[i][j]=999999999;
}
}
queue<node>q;
node t;
t.x=x;
t.y=y;
t.len=0;
Min[x][y]=0;
q.push(t);
while(!q.empty()){
t=q.front();
q.pop();
for(int i=0; i<4; i++){
node t1=t;
t1.x+=tox[i];
t1.y+=toy[i];
if(t1.x<1 || t1.x>n || t1.y<1 || t1.y>n)continue;
if(a[t1.x][t1.y]==a[t.x][t.y])t1.len+=A;
else t1.len+=B;
if(Min[t1.x][t1.y]<=t1.len)continue;
Min[t1.x][t1.y]=t1.len;
q.push(t1);
}
}
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
ans=max(Min[i][j],ans);
}
}
}
int main(){
scanf("%d%d%d\n",&n,&A,&B);
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
char c=getchar();
if(c=='(')a[i][j]=0;
else a[i][j]=1;
}getchar();
}
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
shortest_path(i,j);
}
}
printf("%lld\n",ans);
return 0;
}