【水】java试手—poj 2387

拿水题练下java能力,题目链接:http://poj.org/problem?id=2387

 
这个结果算是写炸了吧……真心感觉Java不好用(应该是我太菜了TAT)
 
 1 import java.math.*;
 2 import java.io.*;
 3 import java.util.*;
 4 
 5 class vec{
 6     int t,w,next;
 7     vec(){
 8         t=0;w=0;next=0;
 9     }
10 }
11 
12 class edge{
13     static int maxn=1005;
14     static int maxm=1000005;
15     static int inf=1000000009;
16     int []head=new int[maxn];
17     int []d=new int[maxn];
18     vec []E=new vec[maxm];
19     int ne;
20     edge(){
21         ne=0;
22         for(int i=0;i<maxn;i++){
23             head[i]=-1;
24             d[i]=inf;
25         }
26         for(int i=0;i<maxm;i++){
27             E[i]=new vec();
28         }
29     }
30     
31     void add_edge(int s,int t,int w){
32         ne++;
33         E[ne].t=t;
34         E[ne].w=w;
35         E[ne].next=head[s];
36         head[s]=ne;
37     }
38 }
39 
40 public class Main {
41     public static void main(String[] args) {
42         Scanner cin=new Scanner(System.in);
43         int n,m;
44         while(cin.hasNext()){
45             m=cin.nextInt(); n=cin.nextInt();
46             edge G=new edge();
47             for(int i=0;i<m;i++){
48                 int s,t,w;
49                 s=cin.nextInt();t=cin.nextInt();w=cin.nextInt();
50                 G.add_edge(s, t, w);
51                 G.add_edge(t, s, w);
52             }
53             G.d[1]=0;
54             Deque<Integer> q=new LinkedList<Integer>();
55             q.add(1);
56             while(!q.isEmpty()){
57                 int now=q.pop();
58                 for(int i=G.head[now];i!=-1;i=G.E[i].next){
59                     int v=G.E[i].t;
60                     if(G.d[v]>G.d[now]+G.E[i].w){
61                         G.d[v]=G.d[now]+G.E[i].w;
62                         q.add(v);
63                     }
64                 }
65             }
66             System.out.println(G.d[n]);
67         }
68     }
69 
70 }
原文地址:https://www.cnblogs.com/hymscott/p/6599222.html