java简直输入输出太可怕了。
于是明明是Nlog(E)的算法就是过不了。
去哭会儿。
import java.io.*;
import java.util.*;
// Flowery Trail
public class Main {
public static void main(String[] args) throws IOException{
Test t = new Test();
t.traiter();
}
}
class Node implements Comparable<Node>{
int dist;
int u;
public Node(int d, int u){
this.dist = d;
this.u = u;
}
@Override
public int compareTo(Node a) {
// TODO Auto-generated method stub
return this.dist - a.dist;
}
public String toString(){
return " " + u + " "+ dist;
}
}
class Test{
BufferedReader f;
PrintWriter out;static int INF = 10000000;
int P, T, st=0, en;
boolean[] visit, cc;
boolean[][] preview;
ArrayList<Integer>[] nei, pre;
int[][] dist, count;
int[] way;
void traiter() throws IOException{
f = new BufferedReader(new InputStreamReader(System.in));
// f = new BufferedReader(new FileReader("A.in"));
run();
// dijkstra with precedents memorized
}
void dijkstra(){
Node[] node = new Node[P];
PriorityQueue<Node> q = new PriorityQueue<Node>();
for(int i = 0; i<P; i++){
node[i] = new Node(way[i], i);
}
q.add(node[0]);
for(int i = 0; i < P; i++){
// find the current nearest with priority queue(then we need to define the data type)
Node t = q.poll();
int u = t.u;
if(u == P-1)
break; // cut
visit[u] = true;
for(int k : nei[u]){
if(visit[k] || dist[u][k] + way[u] > way[k])
continue;
if(dist[u][k] + way[u] == way[k]){
boolean mark = false;
for(int c = 0; c < pre[k].size(); c++){
if(pre[k].get(c) == u)
mark = true;
}
if(!mark)
pre[k].add(u);
}
if(dist[u][k] + way[u] < way[k]){
way[k] = dist[u][k] + way[u];
pre[k] = new ArrayList<Integer>();
pre[k].add(u);
node[k].dist = way[k];
q.add(node[k]);
}
}
}
}
int recount(int v, int num){
if(v == st)
return num;
for(int u : pre[v]){
if(cc[u])
continue;
num += dist[u][v]*count[u][v];
num = recount(u,num);
}
return num;
}
void run() throws IOException{
String l = f.readLine();
int u, v, d, fois = 0;
while(l!=null){
StringTokenizer s = new StringTokenizer(l);
// System.out.println(++fois);
P = Integer.parseInt(s.nextToken()); T = Integer.parseInt(s.nextToken());
dist = new int[P][P];
count = new int[P][P];
visit = new boolean[P];
en = P-1;
way = new int[P];
Arrays.fill(way, INF);
way[0] = 0;
Arrays.fill(visit, false);
nei = new ArrayList[P];
pre = new ArrayList[P];
for(int i = 0; i < P; i++){
nei[i] = new ArrayList<Integer>();
pre[i] = new ArrayList<Integer>();
for(int j = 0; j<P; j++){
dist[i][j]= i==j?0:INF;
count[i][j] = 0;
}
}
for(int i = 0; i<T; i++){
s = new StringTokenizer(f.readLine());
u = Integer.parseInt(s.nextToken());
v = Integer.parseInt(s.nextToken());
d = Integer.parseInt(s.nextToken());
if(d < dist[u][v]){
dist[u][v] = dist[v][u] = d;
count[u][v] = count[v][u] = 1;
}
else if(d == dist[u][v])
count[u][v] ++;
if(u!=v){
nei[u].add(v);
nei[v].add(u);
}
}
// show the distance table
/* for(int i = 0; i<P; i++){
for(int j = 0; j<P; j++)
System.out.print(count[i][j]+ " ");
System.out.println();
}*/
preview = new boolean[P][P];
dijkstra();
cc = new boolean[P];
System.out.println(2*recount(en, 0));
l = f.readLine();
}
}
}