void Kruskal(Graph& G, Edge* &MST)  {
	ParTree<int> A(G.VerticesNum());           //等价类
    MinHeap<Edge> H(G.EdgesNum());        //最小值堆(minheap)    
	MST=new Edge[G.VerticesNum()-1];      //最小支撑树
	int MSTtag=0;                         //最小支撑树边的标号
    for(int i=0; i<G.VerticesNum(); i++)  //将图的所有边插入最小值堆H中
		for(Edge e= G. FirstEdge(i); G.IsEdge(e);e=G. NextEdge(e))
			if(G.FromVertex(e)< G.ToVertex(e))  //因为是无向图,所以应防止重复插入
				H.Insert(e);
	int EquNum=G.VerticesNum();              //开始时有|V|个等价类
    while(EquNum>1)  {                     //合并等价类
		Edge e=H.RemoveMin();               //获得下一条权最小的边
		if(e.weight==INFINITE)  {
			cout << "不存在最小支撑树." <<endl;
            delete [] MST;                     //释放空间
            MST=NULL;                   //MST是空数组
			return;
		}
        int from=G.FromVertex(e);            //记录该条边的信息
        int to= G.ToVertex(e);
        if(A.Different(from,to))  {            //如果边e的两个顶点不在一个等价类
			A.Union(from,to);     //将边e的两个顶点所在的两个等价类合并为一个
			AddEdgetoMST(e,MST,MSTtag++); //将边e加到MST
			EquNum--;                     //将等价类的个数减1
		}
	}
}
//Dijkstra算法,其中参数G是图,参数s是源顶点,D是保存最短距离及其路径的数组
void Dijkstra(Graph& G, int s, Dist* &D)  {
	D = new Dist[G. VerticesNum()];          	// D数组
	for (int i = 0; i < G.VerticesNum(); i++) {   	// 初始化Mark数组、D数组
		G.Mark[i] = UNVISITED;
        D[i].index = i;
        D[i].length = INFINITE;
        D[i].pre = s;
    }
    D[s].length = 0; 
    MinHeap<Dist> H(G. EdgesNum());       	// 最小值堆(minheap)
    H.Insert(D[s]);
	for (i = 0; i < G.VerticesNum(); i++) {
		bool FOUND = false;
        Dist d;
        while (!H.isEmpty())  {
			d = H.RemoveMin(); 
			if(G.Mark[d.index]==UNVISITED) {                //打印出路径信息
				cout<< "vertex index: " <<d.index<<"   ";
				cout<< "vertex pre  : " <<d.pre  <<"   ";
				cout<< "V0 --> V" << d.index <<"  length    : " <<d.length<<endl;
			}
			
			if (G.Mark[d.index] == UNVISITED) { //找到距离s最近的顶点
				FOUND = true;
				break;
			}
        }
		if (!FOUND)
            break;
        int v = d.index;
		G.Mark[v] = VISITED;           		// 把该点加入已访问组
		// 因为v的加入,需要刷新v邻接点的D值
		for (Edge e = G.FirstEdge(v); G.IsEdge(e);e = G.NextEdge(e))
			if (D[G.ToVertex(e)].length > (D[v].length+G.Weight(e))) {
				D[G.ToVertex(e)].length = D[v].length+G.Weight(e);
				D[G.ToVertex(e)].pre = v;
				H.Insert(D[G.ToVertex(e)]);
			}
	}
}