Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
此题神坑,先边数后点数一定要注意啊,附上JAVA版AC代码import java.util.*; public class POJ2387 { public static class Pair implements Comparable<Pair> { int v,w; Pair(int v,int weight) { this.v=v;w=weight; } public int compareTo(Pair x) { return this.w-x.w; } } public static void main(String[] args) { Scanner in=new Scanner(System.in); int t=in.nextInt(),n=in.nextInt(); ArrayList<Pair>[] path=new ArrayList[n+1]; int[] d=new int[n+1]; for(int i=1;i<=n;i++) { path[i]=new ArrayList<Pair>(); d[i]=-1; } for(int i=0;i<t;i++) { int a=in.nextInt(),b=in.nextInt(), c=in.nextInt(); path[a].add(new Pair(b,c)); path[b].add(new Pair(a,c)); } Queue<Pair> q=new PriorityQueue<Pair>(); d[1]=0;q.add(new Pair(1,0)); while(!q.isEmpty()) { Pair a=q.poll(); while(!q.isEmpty()&&a.w>d[a.v]) a=q.poll(); for(int i=0;i<path[a.v].size();i++) { Pair b=path[a.v].get(i); if(d[b.v]==-1||d[b.v]>d[a.v]+b.w) { d[b.v]=d[a.v]+b.w; q.add(new Pair(b.v,d[b.v])); } } } System.out.println(d[n]); } } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator