importjava.util.ArrayList;importjava.util.Arrays;importjava.util.Iterator;importjava.util.LinkedList;importjava.util.List;importjava.util.Scanner;public classMain {private static intn;private final static int MAX =Integer.MAX_VALUE;private static boolean[] finalVex;private static int[] shortPath;private static List>list;public static voidshortPathDij() {
Edge tmp= null;
shortPath= new int[n];int[] tails = new int[n];int[] exp = new int[n];
finalVex= new boolean[n];
Arrays.fill(shortPath, MAX);
Arrays.fill(finalVex,false);
Arrays.fill(exp,0);
shortPath[0] = 0;
tails[0] = 1;while(!finalVex[n-1]) {int index =min(shortPath);if(index == -1)break;
LinkedList p =list.get(index);
Iterator it =p.iterator();int j=0;while(it.hasNext()) {
tmp=it.next();
j=tmp.end;if(finalVex[j])continue;if(tmp.type==1) {int eee = exp[index]+tmp.weight;int sum = shortPath[index]-(int)Math.pow(exp[index], 2)+(int)Math.pow(eee, 2);if(sum
shortPath[j]=sum;
tails[j]= index+1;
exp[j]=eee;
}
}else{if((shortPath[index]+tmp.weight)
shortPath[j]= shortPath[index]+tmp.weight;
tails[j]= index+1;
exp[j]= 0;
}
}
}
}
}private static int min(int[] arr) {int index = -1;for(int i=0; i
index=i;if(index==-1)return -1;for(int i=0; iarr[i]&&!finalVex[i])
index=i;
finalVex[index]= true;returnindex;
}public static voidmain(String[] args) {
Scanner in= newScanner(System.in);
n=in.nextInt();int nums =in.nextInt();
list= new ArrayList<>(n);for(int i=0; i
list.add(new LinkedList());
}for(int i=0; i
list.get(start-1).add(new Edge(type, start-1, end-1, weight));
list.get(end-1).add(new Edge(type, end-1, start-1, weight));
}
shortPathDij();
System.out.println(shortPath[n-1]);
in.close();
}
}classEdge{public inttype;public intstart;public intend;public intweight;public Edge(int type, int start, int end, intweight) {this.type =type;this.start =start;this.end =end;this.weight =weight;
}
}