SW练习_Pro_星际穿越_分层迪杰斯特拉

int要改成long才可以

package com.company.real;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.PriorityQueue;
import java.util.StringTokenizer;
/*
3
6 6 0
1 2 3
5 1 2
5 6 5
3 4 3
6 4 6
2 3 3
1 4
6 6 1
1 2 3
5 1 2
5 6 5
3 4 3
6 4 6
2 3 3
4 1
6 6 2
1 2 3
5 1 2
5 6 5
3 4 3
6 4 6
2 3 3
1 4
#1 9
#2 7
#3 4
*/
//SW练习_Pro_星际穿越_分层迪杰斯特拉
public class XingjiChuanyue {
    static NodeXingji[] nodes;
    static long Max_value=208765432100l;
    public static void main(String[] args) throws Exception{
        System.setIn(new FileInputStream("C:\\Users\\XAGDC\\IdeaProjects\\POJ\\src\\com\\company\\real\\caseXingji.txt"));
        BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st=new StringTokenizer(reader.readLine());
        int T=Integer.parseInt(st.nextToken());
        for (int zz = 0; zz <T ; zz++) {
            st=new StringTokenizer(reader.readLine());
            int N=Integer.parseInt(st.nextToken());//行星的个数,行星的编号是 1~N
            int M=Integer.parseInt(st.nextToken());//路线的个数
            int K=Integer.parseInt(st.nextToken());//穿越包的个数
            nodes=new NodeXingji[N*(K+1)+1];
            for (int i = 0; i <nodes.length ; i++) {
                nodes[i]=new NodeXingji(i);
            }
            for (int i = 0; i <M ; i++) {
                st=new StringTokenizer(reader.readLine());
                int from=Integer.parseInt(st.nextToken());
                int to=Integer.parseInt(st.nextToken());
                int len=Integer.parseInt(st.nextToken());
                //同层不变,不同的层距离变成1
                for (int j = 0; j <=K ; j++) {
                    nodes[from+j*N].sonList.add(new long[]{to+j*N,len});//同层不变
                    nodes[to+j*N].sonList.add(new long[]{from+j*N,len});
                    if(j<K){
                        nodes[from+j*N].sonList.add(new long[]{to+(j+1)*N,1});//不同层变成1
                        nodes[to+j*N].sonList.add(new long[]{from+(j+1)*N,1});
                    }
                }
            }
            st=new StringTokenizer(reader.readLine());
            int start=Integer.parseInt(st.nextToken());
            int end=Integer.parseInt(st.nextToken());
            processDis(start);
            long minLen=Max_value;
            for (int i = 0; i <=K ; i++) {
                if(minLen>nodes[i*N+end].len){
                    minLen=nodes[i*N+end].len;
                }
            }
            System.out.printf("#%d %d\n",(zz+1),minLen);
        }
        reader.close();
    }
    public static void processDis(int start){
        PriorityQueue<Integer> pq=new PriorityQueue();
        nodes[start].len=0;
        nodes[start].poped=false;
        pq.add(start);
        while(!pq.isEmpty()){
            int from=pq.poll();//
            if(nodes[from].poped){
                continue;
            }
            nodes[from].poped=true;
            for (long[] tolen:nodes[from].sonList) {
                int to=(int)tolen[0];
                long len=tolen[1];
                if(nodes[to].len > nodes[from].len+len){
                    nodes[to].len = nodes[from].len+len;
                    nodes[to].poped=false;
                    pq.add(to);
                }
            }
        }
    }
    static class NodeXingji implements Comparable<NodeXingji>{
        int index;
        long len;
        boolean poped;
        List<long[]> sonList;
        public NodeXingji(int index){
            this.index=index;
            this.len=Max_value;
            this.sonList=new ArrayList<>();
        }
        @Override
        public int compareTo(NodeXingji o) {
            if(this.len-o.len>0){
                return 1;
            }else if(this.len-o.len==0){
                return 0;
            }else{
                return -1;
            }
            //return this.len-o.len;
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值