public static void main(String args[])
{
float max=Float.MAX_VALUE;
float[][] a=new float[][]{{0,0,0,0,0,0},
{0,max,10,max,15,50},
{0,max,max,30,max,max},
{0,max,max,max,max,10},
{0,max,max,5,max,25},
{0,max,max,max,max,max}};
int v=1;
int n=5;
float[] dist=new float[n+1];
int[] prev=new int[n+1];
dijkstra(v,a,dist,prev);
for (int i=1;i<=n;i++)
if (i!=v)
System.out.println(v+"->"+i+" : "+dist[i]);
}
}
3.最小生成树
public class EdgeNode implements Comparable
{
float weight;
int u,v;
EdgeNode(int uu,int vv,float ww)
{
u=uu;
v=vv;
weight=ww;
}
public int compareTo(Object x)
{
double xw=((EdgeNode)x).weight;
if (weight
unionTree=new Tree[n+1];
root=new Tree[n+1];
for (int i=1;i<=n;i++)
{
unionTree[i]=new Tree();
unionTree[i].node=i;
root[i]=unionTree[i];
}
this.n=n;
}
public int find(int nodeindex)
{
if (nodeindex>n || nodeindex<1) return -1;
Tree temp=unionTree[nodeindex];
while (temp.upper!=null) temp=temp.upper;
return temp.node;
}
public void union(int a,int b)
{
Tree newtree=new Tree();
newtree.node=root[a].node;
root[a].upper=newtree;
root[a]=newtree;
root[b].upper=newtree;
root[b]=null;
}
}
public class MinHeap {
static Comparable[] heap;
static int Len;
public MinHeap()
{
heap=null;
Len=0;
}
public MinHeap(int n)
{
heap=new Comparable[n];
Len=0;
}
/* 初始化堆结构 */
public void initialize(Comparable[] comp, int n)
{
if (heap==null || heap.length
Len=n;
for (int i=0;i0 && heap[j].compareTo(heap[j/2])==-1)
{
Comparable temp=heap[j];
heap[j]=heap[j/2];
heap[j/2]=temp;
j=j/2;
}
}
}
/* 输出最小元素,并重建堆 */
public Comparable removeMin()
{
Comparable Min=heap[0];
heap[0]=heap[Len-1];
Len--;
int j=0;
while (j=Len)) // 如果heap[j*2]最小
Comparable temp=heap[j];
heap[j]=heap[j*2];
heap[j*2]=temp;
j=j*2;
}
else
if (j*2+1=0)
// 如 果
&&
heap[j*2+1]最小
{
{
Comparable temp=heap[j];
heap[j]=heap[j*2+1];
heap[j*2+1]=temp;
j=j*2+1;
}
else break;