设为首页 加入收藏

TOP

poj3026Borg Maze(bfs预处理+最小生成树)(二)
2015-07-20 17:58:53 来源: 作者: 【 】 浏览:5
Tags:poj3026Borg Maze bfs 处理 最小 生成
{ memset(Hash,false,sizeof(Hash)); int now,tmp,ans=0,cur; for (int i=2;i<=cal;i++) d[i]=INF; d[1]=0; for(int i=1;i<=cal;i++) { tmp=INF; for(int j=1;j<=cal;j++) { if(!Hash[j]&&tmp>d[j]) { tmp=d[j]; now=j; } } ans=ans+tmp; Hash[now]=true; for(int j=1;j<=cal;j++) { if(!Hash[j]&&d[j]>gra[now][j]) d[j]=gra[now][j]; } } return ans; } bool check(int x,int y) { if(x>=1&&x<=n&&y>=1&&y<=m&&!vis[x][y]&&map[x][y]>=0) return true; return false; } void bfs(int sx,int sy) { queue Q; while(!Q.empty()) Q.pop(); memset(vis,false,sizeof(vis)); Point current,next; current.x=sx; current.y=sy; current.time=0; vis[sx][sy]=true; Q.push(current); while(!Q.empty()) { current=Q.front(); Q.pop(); for(int i=0;i<4;i++) { next.x=current.x+dx[i]; next.y=current.y+dy[i]; if(check(next.x,next.y)) { next.time=current.time+1; if(map[next.x][next.y]>=1) { gra[map[next.x][next.y]][map[sx][sy]]=next.time; gra[map[sx][sy]][map[next.x][next.y]]=next.time; } vis[next.x][next.y]=true; Q.push(next); } } } } void solve() { for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) if(map[i][j]>0) bfs(i,j); cout<

首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇杭电 2149(巴什博弈) 下一篇UVA 11605 - Lights inside a 3d ..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: