关闭

HDU2544最短路

171人阅读 评论(0) 收藏 举报
分类:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;
import java.util.StringTokenizer;

import javax.management.Query;


public class Main {

        public static void main(String[] args) {
                InputReader in = new InputReader(System.in) ;
                PrintWriter out = new PrintWriter(System.out);

                int n , m  ;
                while((n = in.nextInt()) != 0 && (m = in.nextInt()) != 0){
                       new Task().solve(n, m, in, out) ;
                }

                out.flush() ;
        }

}

class Task{

      static  class E{
              int v ;
              int w ;
              public E(int v , int w){
                     this.v = v ;
                     this.w = w ;
              }
      }

      ArrayList<E>[] adj ;
      int[] dist ;
      boolean[] inq ;


      void solve(int n , int m , InputReader in , PrintWriter out){
           adj = new ArrayList[n+1] ;  
           for(int i = 1 ; i <= n ; i++) adj[i] = new ArrayList<E>();

           for(int i = 0 ; i < m ; i++){
                 int u = in.nextInt() ;
                 int v = in.nextInt() ;
                 int w = in.nextInt() ;
                 adj[u].add(new E(v, w)) ;
                 adj[v].add(new E(u, w)) ;
           }
           dist = new int[n+1] ;
           Arrays.fill(dist, Integer.MAX_VALUE) ;
           inq = new boolean[n+1] ;
           Arrays.fill(inq, false) ;
           dist[1] = 0 ;
           inq[1] = true ;
           Queue<Integer> que = new LinkedList<Integer>() ;
           que.add(1) ;
           while(! que.isEmpty()){
                int u = que.poll() ;
                inq[u] = false ;
                for(E e : adj[u]){
                      if(dist[u] + e.w < dist[e.v]){
                            dist[e.v] = dist[u] + e.w ;
                            if(! inq[e.v]){
                                 inq[e.v] = true ;
                                 que.add(e.v) ;
                            }
                      }
                }
           }

           out.println(dist[n]) ;
      }
} 

class InputReader{
    public BufferedReader reader;
    public StringTokenizer tokenizer;

    public InputReader(InputStream stream){
           reader = new BufferedReader(new InputStreamReader(stream), 32768);
           tokenizer = null;
    }

    public String next(){
        while(tokenizer == null || !tokenizer.hasMoreTokens()){
            try{
                tokenizer = new StringTokenizer(reader.readLine());
            }catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return tokenizer.nextToken();
    }

    public int nextInt() {
        return Integer.parseInt(next());
    }

}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:214095次
    • 积分:9516
    • 等级:
    • 排名:第1880名
    • 原创:668篇
    • 转载:0篇
    • 译文:0篇
    • 评论:55条
    文章分类
    最新评论