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;
}
}
}