{ 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<
|