最小生成树(城市之间的最短距离)
来源:互联网 发布:故宫淘宝营销策略分析 编辑:程序博客网 时间:2024/06/06 02:33
package coding;import java.util.Scanner;/* * 最小生成树求解城市间的最短距离 */class GraphMatrix1{ static final int MaxNum=20; char[] Vertex=new char[MaxNum]; int GType; int VertexNum; int EdgeNum; int [][] EdgeWeight=new int [MaxNum][MaxNum]; int [] isTra=new int[MaxNum];}public class cha07_minTree { static final int MaxValue=65535; static final int USED=0; static final int Nol=-1; static Scanner input =new Scanner(System.in); static void CreateGraph(GraphMatrix1 GM){ int i,j,k; int weight; char EstartV,EendV; System.out.printf("输入图中各顶点的信息:\n"); for(i=0;i<GM.VertexNum;i++){ System.out.printf("第%d个顶点:",i+1); GM.Vertex[i]=(input.next().toCharArray()[0]); } System.out.printf("输入构成各边的顶点及权值:\n"); for(k=0;k<GM.EdgeNum;k++){ System.out.printf("第%d条边:",k+1); EstartV=input.next().charAt(0); EendV=input.next().charAt(0); weight=input.nextInt(); for(i=0;EstartV!=GM.Vertex[i];i++); for(j=0;EendV!=GM.Vertex[j];j++); GM.EdgeWeight[i][j]=weight; if(GM.GType==0){ GM.EdgeWeight[i][j]=weight; } } } static void clearGraph(GraphMatrix1 GM){ int i,j; for(i=0;i<GM.VertexNum;i++){ for(j=0;j<GM.VertexNum;j++){ GM.EdgeWeight[i][j]=MaxValue; } } } static void OutGraph(GraphMatrix1 GM){ int i,j; for(j=0;j<GM.VertexNum;j++){ System.out.printf("\t%c",GM.Vertex[j]); } System.out.println(); for(i=0;i<GM.VertexNum;i++){ System.out.printf("%c",GM.Vertex[i]); for(j=0;j<GM.VertexNum;j++){ if(GM.EdgeWeight[i][j]==MaxValue){ System.out.printf("\tZ"); } else{ System.out.printf("\t%d",GM.EdgeWeight[i][j]); } } System.out.println(); } } static void primGraph(GraphMatrix1 GM){ int i,j,k,min,sum; int[]weight=new int[GraphMatrix1.MaxNum]; char[]vtempx=new char[GraphMatrix1.MaxNum]; sum=0; for(i=1;i<GM.VertexNum;i++){ weight[i]=GM.EdgeWeight[0][i]; if(weight[i]==MaxValue){ vtempx[i]=(char)Nol; }else{ vtempx[i]=GM.Vertex[0]; } } vtempx[0]=USED; weight[0]=MaxValue; for(i=1;i<GM.VertexNum;i++){ min=weight[0]; k=i; for(j=0;j<GM.VertexNum;j++){ if(weight[j]<min&&vtempx[j]>0){ min=weight[j]; k=j; } } sum+=min; System.out.printf("(%c,%c),",vtempx[k],GM.Vertex[k]); vtempx[k]=USED; weight[k]=MaxValue; for(j=0;j<GM.VertexNum;j++){ if(GM.EdgeWeight[k][j]<weight[j]&&vtempx[j]!=0){ weight[j]=GM.EdgeWeight[k][j]; vtempx[j]=GM.Vertex[k]; } } } System.out.println("最小生成树的总权值:"+sum); } public static void main(String[] args) { // TODO Auto-generated method stub GraphMatrix1 GM=new GraphMatrix1(); char again; String go; System.out.println("需找最小生成树"); do{ System.out.printf("请输入生成图的类型 :\n"); GM.GType=input.nextInt(); System.out.printf("输入图的顶点数量:\n"); GM.VertexNum=input.nextInt(); System.out.printf("输入图的边数量:\n"); GM.EdgeNum=input.nextInt(); clearGraph(GM); CreateGraph(GM); System.out.println("最小生成树的边为:"); primGraph(GM); System.out.println("继续玩吗?(y/n)"); go=input.next(); }while(go.equalsIgnoreCase("y")); System.out.println("游戏结束!"); }}
0 0
- 最小生成树(城市之间的最短距离)
- 求城市里两点之间的最短距离.
- 链接三维空间中n个点的最短距离 最小生成树(krukal算法)
- 字符串之间的最短距离
- c/c++常用算法(15) -- 经典数据结构(城市之间的最短距离问题)
- 携程初赛 携程全球数据中心建设 球面上两点的最短距离 + 最小生成树
- 两个面之间的最短距离算法(记录)
- Floyd(各对顶点之间的最短距离)
- 两个单词之间的最短距离
- 1与2之间的最短距离
- 链接城市最小生成树
- poj3532求生成树中最大权与最小权只差最小的生成树+hoj1598俩个点之间的最大权与最小权只差最小的路经。
- 城市间最短距离Prolog源程序
- hdu12823(两凸包之间最短距离)
- 最小生成树的应用--城市高速公路问题
- Floyd算法(求每一对顶点之间的最短距离)
- Floyd算法,求图中两个点之间的最短距离
- UVa10075 - Airlines(所有点对之间的最短距离)
- 产品与运营之用户激励机制
- 使用jsp/servlet简单实现文件上传所遇到的问题
- 哈希表
- 消费者JmsListener应用源码浅析
- php分页显示数据库内容
- 最小生成树(城市之间的最短距离)
- 配置方法数超过 64K 的应用
- copy构造函数的调用时机1和2
- 222. Count Complete Tree Nodes
- APP开发实战148-使用AOP技术输出Log的具体实现
- 【WEB】前端系统配色方案(全览)
- Android底部/顶部滑动菜单,方便调用的工具类
- Java Web乱码 解决request中文乱码的问题
- gradle学习笔记之hook task