克鲁斯卡尔公交车最短路径问题

主要是在于记录头和尾

使用一个数组来记录该点的尽头

//核心代码段
public static int findDian(int[] ints, int index){
        while (ints[index] != 0){
            index = ints[index];
        }
        return index;
    }

package 十大算法;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Objects;

public class 克鲁斯卡尔公交车最短路径问题 {
    public static void main(String[] args) {

        line line1 = new line('A', 'B', 12);
        line line2 = new line('A', 'F', 16);
        line line3 = new line('A', 'G', 14);
        line line4 = new line('B', 'F', 7);
        line line5 = new line('B', 'C', 10);
        line line6 = new line('C', 'F', 6);
        line line7 = new line('C', 'E', 5);
        line line8 = new line('C', 'D', 3);
        line line9 = new line('D', 'E', 4);
        line line10 = new line('E', 'F', 2);
        line line11 = new line('E', 'G', 8);
        line line12 = new line('F', 'G', 9);

        ArrayList<line> lines = new ArrayList<>();
        ArrayList<line> linesss = new ArrayList<>();
        lines.add(line1);
        lines.add(line2);
        lines.add(line3);
        lines.add(line4);
        lines.add(line5);
        lines.add(line6);
        lines.add(line7);
        lines.add(line8);
        lines.add(line9);
        lines.add(line10);
        lines.add(line11);
        lines.add(line12);

        Character[] zimubiao = new Character[]{'A', 'B', 'C', 'D', 'E', 'F', 'G'};

        fsort(lines);
        f(lines, linesss, zimubiao);
    }

    public static void f(ArrayList<line> lines, ArrayList<line> linesss, Character[] zimubiao) {
        int[] ints = new int[zimubiao.length];
        for (int i = 0; i < lines.size(); i++){
            line line1 = lines.get(i);

            int start = zhaoshu(line1.left, zimubiao);
            int end = zhaoshu(line1.right, zimubiao);


//            主要是这个点,就是拿一个数组去存储对应的结束点,我的妈呀,太绝了!!!!!!!!!!!!!!!!!!!!!!
//            进入ints里面找对应的点
            int dianStart = findDian(ints, start);
            int dianEnd = findDian(ints, end);

            if (dianStart != dianEnd){
                ints[dianStart] = dianEnd;
                linesss.add(line1);
            }

        }

        printList(linesss);

    }

    public static int findDian(int[] ints, int index){
        while (ints[index] != 0){
            index = ints[index];
        }
        return index;
    }

    public static int zhaoshu(Character a, Character[] zimubiao){
        for (int i = 0; i < zimubiao.length; i++){
            if (zimubiao[i] == a){
                return i;
            }
        }
        return -1;
    }

//    给边排序
    public static void fsort(ArrayList<line> lines){
        for (int i = 0; i < lines.size()-1; i++){
            for (int j = i+1; j < lines.size(); j++){
                if (lines.get(i).weight > lines.get(j).weight){
                    line temp = lines.get(i);
                    lines.set(i, lines.get(j));
                    lines.set(j, temp);
                }
            }
        }
//        printList(lines);
    }

    public static void printList(ArrayList<line> lines){
        Iterator<line> iterator = lines.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
        }
    }

}



class line{
    Character left;
    Character right;
    int weight;

    public line(Character left, Character right, int weight) {
        this.left = left;
        this.right = right;
        this.weight = weight;
    }

    @Override
    public String toString() {
        return "line{" +
                "left=" + left +
                ", right=" + right +
                ", weight=" + weight +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        line line = (line) o;
        return weight == line.weight && Objects.equals(left, line.left) && Objects.equals(right, line.right);
    }

    @Override
    public int hashCode() {
        return Objects.hash(left, right, weight);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值