展开ljdwx
求图论算法java实现packagetest
importjava.io.BufferedReader;
importjava.io.FileReader;
importjava.io.IOException;
importjava.util.ArrayList;
importjava.util.Comparator;
importjava.util.HashMap;
importjava.util.Map;
importjava.util.PriorityQueue;
importjava.util.Queue;
publicclassMinSpanningTree{
classEdge{//内部类定义边缘的数据结果
intu,v,weight;
}
MapnodeFather=newHashMap (;
intcnt=0,nodeCnt=0;
publicminspanningtree (字符串路径) {
try{
字符串str;
StringwjddystrArray;
wile((str=br.readline ) )!=null ) {
strarray=str.split((s ) );
edges.add(CNT,newEdge ) );
edges.get(CNT ).u=integer.parseint ) strarray[0];
edges.get(CNT ).v=integer.parseint ) strarray[1];
edges.get(CNT ).weight=integer.parseint ) strarray[2];
if (! node father.contains key (edges.get (CNT ).u ) }
nodefather.put(Edges.get(CNT ).u,Edges.get ).u ); //初始化,father[i]=i;
}
if (! node father.contains key (edges.get (CNT ).v ) }
nodefather.put(Edges.get(CNT ).v,Edges.get ).v );
}
}
br.close (;
}catch(ioexceptione ) {
e .打印任务跟踪(;
}
}
publicbooleanunion(intu,intv ) )//并行操作
INTA=find(u;
intb=find(v );
if(a!=b ) {
nodefather.put(a,b );
返回真;
}
返回假;
}
公共查找(intx )//检查操作
if(x!=nodefather.get(x ) }{
nodefather.put(x,find ) node father.get (x );
}
returnodefather.get(x;
}
publicarraylistgetminspanningtree (
queuefsqueue=newpriorityqueue (1000,//边缘e68a 843231愚人节231343134313032313333133331333313333133313333133333313333333333313333331333333331333333333133333313333333133333333333
新公司
公共公司(edgeedgeone,Edgekxdjc ) {
edge one.weightkxdjc.weight ) )。
返回1;
elseif(edgeone.weight
else
返回0;
}
);
for(inti=0; I
}
while (! FsQueue.isEmpty () ) /遍历每条边
EdgeEdge=FsQueue.poll (;
if(union(edge.u,Edge.v ) ) }
result.add(edge;
}else{
连续;
}
}
返回结果;
}
publicstaticvoidmain (stringwjdyargs ) {
for(inti=0; I
}
}
}