Question Surface
We can easily think of the difference constraint, but it seems that we are not easy to start with after creating the graph, because we can only get the size relationship between weights, and cannot easily get the specific weight of each weight.
So we have a magical idea: since we don't need a specific weight, we just need to find the relationship between weights, because there are only three weights that can be discussed. Specifically, we use $ Maxx [I] [J] $ to represent the maximum value of $ I-j $, and $ mini [I] [J] $ to represent the minimum value of $ I-j $, run Floyd to get the relationship between weights, and then check the weights directly by $ n ^ 2 $.
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 using namespace std; 5 const int N=55,M=2550; 6 int maxx[N][N],mini[N][N]; 7 int n,a,b,cnt,ans1,ans2,ans3; 8 char rd[N]; 9 int main()10 {11 scanf("%d%d%d",&n,&a,&b);12 for(int i=1;i<=n;i++)13 {14 scanf("%s",rd+1);15 for(int j=1;j<=n;j++)16 if(rd[j]==‘+‘) maxx[i][j]=2,mini[i][j]=1;17 else if(rd[j]==‘-‘) maxx[i][j]=-1,mini[i][j]=-2;18 else if(rd[j]==‘=‘||i==j) maxx[i][j]=mini[i][j]=0;19 else maxx[i][j]=2,mini[i][j]=-2;20 }21 for(int k=1;k<=n;k++)22 for(int i=1;i<=n;i++)23 for(int j=1;j<=n;j++)24 if(i!=j&&i!=k&&j!=k)25 {26 maxx[i][j]=min(maxx[i][j],maxx[i][k]+maxx[k][j]);27 mini[i][j]=max(mini[i][j],mini[i][k]+mini[k][j]);28 }29 for(int i=1;i<=n;i++)30 if(i!=a&&i!=b)31 for(int j=i+1;j<=n;j++)32 if(j!=a&&j!=b)33 {34 ans1+=(mini[a][i]>maxx[j][b]||mini[a][j]>maxx[i][b]);35 ans3+=(maxx[a][i]<mini[j][b]||maxx[a][j]<mini[i][b]); 36 ans2+=(maxx[a][i]==mini[a][i]&&maxx[j][b]==mini[j][b]&&maxx[a][i]==mini[j][b])||37 (maxx[a][j]==mini[a][j]&&maxx[i][b]==mini[i][b]&&maxx[a][j]==mini[i][b]);38 }39 printf("%d %d %d",ans1,ans2,ans3);40 return 0;41 }View code
Problem solving: scoi 2008 balances