import java.io.BufferedInputStream;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;
public class Main{
static int []value ,root;
static int[][]map,tag,dis;
static char[][] cmap;
static int[]xx = {0,0,1,-1};
static int[]yy = {1,-1,0,0};
static int n,m,sx,sy,a,b,con;
static private final int inf= 0x3f3f3f;
static class e implements Comparable<e>{ // 用于bfs
int sx,sy,x ,y,step;
public e(int sx, int sy,int x,int y,int step) {
this.sx = sx;
this.sy =sy;
this.x=x;
this.y=y;
this.step=step;
}
@Override
public int compareTo(e o) {
return this.step- o.step;
}
}
static class e2 implements Comparable<e2>{ //用于保存结果
int u,v,w;
public e2(int u,int v,int w) {
this.u =u;
this.v= v;
this.w = w;
}
@Override
public int compareTo(e2 o) {
return this.w - o .w;
}
}
static PriorityQueue<e2> es = new PriorityQueue<e2>(); //存放结果边
static void bfs() {
es.clear();
con=1;
for(int i=0;i<2000;i++) for(int j=0;j<2000;j++) {
tag[i][j]=0; dis[i]j]=inf;
}
PriorityQueue<e> q = new PriorityQueue<e>();
dis[sx][sy]=0;
q.add(new e(sx,sy,sx,sy,0));
tag[sx][sy]=con++;
while(!q.isEmpty()) {
e temp = q.poll();
for(int i=0;i<4;i++) {
int x2 = temp.x+xx[i];
int y2= temp.y+yy[i];
if(x2 >=1 &&x2<=b && y2>=1 && y2 <= a && cmap[x2][y2]!='#') {
if(temp.step +1 < dis[x2][y2]) {
if(x2==temp.sx && y2 ==temp.sy) continue;
dis[x2][y2]=temp.step+1;
if(cmap[x2][y2]=='A') {
if(tag[x2][y2]==0)tag[x2][y2]=con++;
es.add(new e2(tag[temp.sx][temp.sy], tag[x2][y2], temp.step+1));
q.add(new e(x2,y2,x2,y2,0));
}else {
q.add(new e(temp.sx,temp.sy,x2,y2,dis[x2][y2]));
}
}
}
}
}
}
static int find(int a) {
int temp;
if(root[a]!=a) {
temp = root[a];
root[a]= find(root[a]);
value[a]+=root[temp];
}
return root[a];
}
static void union(int a ,int b) {
int roota = find(a);
int rootb = find(b);
if(value[roota] >=value[rootb]) {
root[rootb]=roota;
if(value[roota]==value[rootb]) value[roota]++;
}else {
root[roota]=rootb;
}
}
static int kruskal() {
int res=0;
int con2 =0;
for(int i=1;i<2000;i++) {
root[i]=i;
value[i]=0;
}
while(!es.isEmpty()) {
e2 temp =es.poll();
int roota = find(temp.u);
int rootb = find(temp.v);
if(roota==rootb)continue;
else {
union(roota,rootb);
res+=temp.w;
con2++;
if(con2==con-2) break;
}
}
return res;
}
public static void main(String[] args) {
Scanner sc=new Scanner(new BufferedInputStream(System.in));
dis= new int[2000][2000];
value = new int[2000];
root = new int [2000];
cmap = new char[2000][2000];
tag = new int[2000][2000];
int test= sc.nextInt();
String s ,s2;
while(test-->0) {
s2="";
while(s2.length()==0)s2=sc.nextLine();
String ss[] = s2.split(" ");
a =Integer.parseInt(ss[0]);
b =Integer.parseInt(ss[1]);
con = 0;
for(int i=1;i<=b;i++) {
s="";
while(s.length()==0) s=sc.nextLine();
if(s.length()<a)for(int j=0;j<a-s.length();j++) s+=" ";
s= s.substring(0, a);
for(int j=1;j<=a;j++) {
cmap[i][j]= s.charAt(j-1);
if(cmap[i][j]=='S') {
sx=i;
sy=j;
}
}
}
bfs();
System.out.println(kruskal());
}
}
}
poj3026-Java-bfs生成路径+krus板
最新推荐文章于 2021-03-18 18:01:51 发布