Run ID 作者 问题 语言 测评结果 分数 时间 内存 代码长度 提交时间
55132 xujindong 最小生成树 (mst) C++ 解答错误 30 169 MS 132904 KB 4120 2022-08-09 11:33:59

Tests(3/10):


#include<bits/stdc++.h> using namespace std; int n,m; char s[55][55]; vector<pair<int,int> >v[3]; void bfs(int x,int y,char c,int cnt){ queue<pair<int,int> >q; q.push(make_pair(x,y)),s[x][y]=c,v[cnt].push_back(make_pair(x,y)); while(!q.empty()){ int nx=q.front().first,ny=q.front().second; q.pop(),s[nx][ny]=c,v[cnt].push_back(make_pair(nx,ny)); if(nx<n&&s[nx+1][ny]=='X')q.push(make_pair(nx+1,ny)); if(nx>1&&s[nx-1][ny]=='X')q.push(make_pair(nx-1,ny)); if(ny<m&&s[nx][ny+1]=='X')q.push(make_pair(nx,ny+1)); if(ny>1&&s[nx][ny-1]=='X')q.push(make_pair(nx,ny-1)); } } void floodfill(int cnt=0){ for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)if(s[i][j]=='X')cnt++,bfs(i,j,(char)(cnt+48),cnt-1); } int dis(pair<int,int>a,pair<int,int>b){ return abs(a.first-b.first)+abs(a.second-b.second)-1; } int dis12(){ int minn=1145141919,ans=1145141919; pair<int,int>min1,min2; for(int i=0;i<v[0].size();i++)for(int j=0;j<v[1].size();j++)if(dis(v[0][i],v[1][j])<minn)minn=dis(v[0][i],v[1][j]),min1=v[0][i],min2=v[1][j]; for(int i=0;i<v[2].size();i++)for(int j=0;j<v[0].size();j++)ans=min(ans,dis(v[2][i],v[0][j])); for(int i=0;i<v[2].size();i++)for(int j=0;j<v[1].size();j++)ans=min(ans,dis(v[2][i],v[1][j])); int l=min(min1.first,min2.first),r=max(min1.first,min2.first),u=min(min1.second,min2.second),d=max(min1.second,min2.second); for(int i=0,x,y;i<v[2].size();i++){ x=v[2][i].first,y=v[2][i].second; if(x>=l&&x<=r&&y>=u&&y<=d)return minn; if(x<l&&y<u)ans=min(ans,l-x+u-y-1); if(x>r&&y<u)ans=min(ans,x-r+u-y-1); if(x<l&&y>d)ans=min(ans,l-x+y-d-1); if(x>r&&y>d)ans=min(ans,x-r+y-d-1); if(x<l&&y>=u&&y<=d)ans=min(ans,l-x+1); if(x>r&&y>=u&&y<=d)ans=min(ans,x-r+1); if(y<u&&x>=l&&x<=r)ans=min(ans,u-y+1); if(y>d&&y>=u&&y<=d)ans=min(ans,y-d+1); } return ans+minn; } int dis13(){ int minn=1145141919,ans=1145141919; pair<int,int>min1,min2; for(int i=0;i<v[0].size();i++)for(int j=0;j<v[2].size();j++)if(dis(v[0][i],v[2][j])<minn)minn=dis(v[0][i],v[2][j]),min1=v[0][i],min2=v[2][j]; for(int i=0;i<v[1].size();i++)for(int j=0;j<v[0].size();j++)ans=min(ans,dis(v[1][i],v[0][j])); for(int i=0;i<v[1].size();i++)for(int j=0;j<v[2].size();j++)ans=min(ans,dis(v[1][i],v[2][j])); int l=min(min1.first,min2.first),r=max(min1.first,min2.first),u=min(min1.second,min2.second),d=max(min1.second,min2.second); for(int i=0,x,y;i<v[1].size();i++){ x=v[1][i].first,y=v[1][i].second; if(x>=l&&x<=r&&y>=u&&y<=d)return minn; if(x<l&&y<u)ans=min(ans,l-x+u-y-1); if(x>r&&y<u)ans=min(ans,x-r+u-y-1); if(x<l&&y>d)ans=min(ans,l-x+y-d-1); if(x>r&&y>d)ans=min(ans,x-r+y-d-1); if(x<l&&y>=u&&y<=d)ans=min(ans,l-x+1); if(x>r&&y>=u&&y<=d)ans=min(ans,x-r+1); if(y<u&&x>=l&&x<=r)ans=min(ans,u-y+1); if(y>d&&y>=u&&y<=d)ans=min(ans,y-d+1); } return ans+minn; } int dis23(){ int minn=1145141919,ans=1145141919; pair<int,int>min1,min2; for(int i=0;i<v[1].size();i++)for(int j=0;j<v[2].size();j++)if(dis(v[1][i],v[2][j])<minn)minn=dis(v[1][i],v[2][j]),min1=v[1][i],min2=v[2][j]; for(int i=0;i<v[0].size();i++)for(int j=0;j<v[1].size();j++)ans=min(ans,dis(v[0][i],v[1][j])); for(int i=0;i<v[0].size();i++)for(int j=0;j<v[2].size();j++)ans=min(ans,dis(v[0][i],v[2][j])); int l=min(min1.first,min2.first),r=max(min1.first,min2.first),u=min(min1.second,min2.second),d=max(min1.second,min2.second); for(int i=0,x,y;i<v[0].size();i++){ x=v[0][i].first,y=v[0][i].second; if(x>=l&&x<=r&&y>=u&&y<=d)return minn; if(x<l&&y<u)ans=min(ans,l-x+u-y-1); if(x>r&&y<u)ans=min(ans,x-r+u-y-1); if(x<l&&y>d)ans=min(ans,l-x+y-d-1); if(x>r&&y>d)ans=min(ans,x-r+y-d-1); if(x<l&&y>=u&&y<=d)ans=min(ans,l-x+1); if(x>r&&y>=u&&y<=d)ans=min(ans,x-r+1); if(y<u&&x>=l&&x<=r)ans=min(ans,u-y+1); if(y>d&&y>=u&&y<=d)ans=min(ans,y-d+1); } return ans+minn; } int main(){ cin>>n>>m; for(int i=1;i<=n;i++)for(int j=1;j<=m;j++)cin>>s[i][j]; return floodfill(),cout<<min(dis12(),min(dis23(),dis13()))<<'\n',0; }


测评信息: