import java.util.*; import java.lang.*; import java.awt.datatransfer.FlavorEvent; import java.io.*; import java.security.spec.ECField; import java.util.List; import java.util.Scanner; import java.io.*; //public class Main{ // public static void main(String []args){ // // } // //} public class Main{ static class Pair implements Comparable<Pair>{ int w,x; Pair(){ } Pair(int a_,int b_){ w=a_;x=b_; } @Override public int compareTo(Pair o) { return w-o.w; } } static int []dis = new int [1010]; static int INF = 0x3f3f3f3f; static int [] to = new int [1010]; static int [] w = new int [1010]; static int [] nxt = new int [1010]; static int [] head = new int [1010]; static int [] vis = new int [1010]; static int cnt = 0; static PriorityQueue<Pair> q = new PriorityQueue<>(); public static void ADD(int u,int v,int ww){ to[cnt]=v; w[cnt]=ww; nxt[cnt]=head[u]; head[u]=cnt;cnt++; } public static int diji(int st,int en){ Arrays.fill(dis,INF); Arrays.fill(vis,0); dis[st]=0; q.add(new Pair(0,st)); while (!q.isEmpty()){ Pair t= q.poll(); if(vis[t.x]==1)continue; vis[t.x]=1; for(int i=head[t.x];i!=-1;i=nxt[i]){ int v = to[i]; if(dis[v]>t.w+w[i]){ dis[v] = t.w+w[i]; q.add(new Pair(dis[v],v)); } } } return dis[en]; } public static void main(String []args){ Scanner sc = new Scanner(System.in); Arrays.fill(head,-1); for(int i=0;i<4;i++){ int u,v,ww; u=sc.nextInt(); v=sc.nextInt(); ww=sc.nextInt(); ADD(u,v,ww); ADD(v,u,ww); } System.out.println(diji(0,3)); for(int i=0;i<4;++i){ System.out.print(dis[i]+" "); } } }
样例:
0 1 1
1 2 2
2 3 1
1 3 6
输出 4