Silver Cow Party java优先队列实现

题目链接:Silver Cow Party

两次迪杰斯特拉即可。这次使用了优先队列。

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());

        int N = Integer.parseInt(st.nextToken());
        int M = Integer.parseInt(st.nextToken());
        int X = Integer.parseInt(st.nextToken());

        ArrayList<Edge>[] roadOut = new ArrayList[N+1];
        //ArrayList<Edge>[] roadBack = new ArrayList[N+1];
        for(int i =1;i<=M;i++){
            st = new StringTokenizer(br.readLine());
            int start = Integer.parseInt(st.nextToken());
            int end = Integer.parseInt(st.nextToken());
            int weight = Integer.parseInt(st.nextToken());
            if(roadOut[start] == null){
                roadOut[start] = new ArrayList<Edge>();
            }
           /* if(roadBack[end] == null){
                roadBack[end] = new ArrayList<Edge>();
            }*/
            roadOut[start].add(new Edge(end,weight));
            //roadBack[end].add(new Edge(start,weight));
        }
        int[] visited = new int[N+1];
        //go out search
        int[] goOutList = new int[N+1];
        for(int m=1;m<=N;m++){
            Arrays.fill(visited,100000000);
            visited[m]=0;
            PriorityQueue<Edge> pq = new PriorityQueue<Edge>();
            Edge init = new Edge(m,0);
            pq.add(init);
            Edge pollEdge;
            int newDist;
            while(!pq.isEmpty()){
                pollEdge = pq.poll();
                for(Edge nextEdge:roadOut[pollEdge.end]){
                    newDist = visited[pollEdge.end]+nextEdge.weight;
                    if(newDist<visited[nextEdge.end]){
                        visited[nextEdge.end] = newDist;
                        pq.add(nextEdge);
                    }
                }
            }
            goOutList[m] = visited[X];
        }
        //return search
        int[] returnList = new int[N+1];
        visited = new int[N+1];
        for(int m = 1;m<=N;m++){
            Arrays.fill(visited,100000000);
            visited[X]=0;
            PriorityQueue<Edge> pq = new PriorityQueue<Edge>();
            Edge init = new Edge(X,0);
            pq.add(init);
            Edge pollEdge;
            int newDist;
            while(!pq.isEmpty()){
                pollEdge = pq.poll();
                for(Edge nextEdge:roadOut[pollEdge.end]){
                    newDist = visited[pollEdge.end]+nextEdge.weight;
                    if(newDist<visited[nextEdge.end]){
                        visited[nextEdge.end] = newDist;
                        pq.add(nextEdge);
                    }
                }
            }
            returnList[m] = visited[m];
        }

        int max=0;
         for(int m = 1;m<=N;m++){
            max = Math.max(goOutList[m]+returnList[m],max);
        }
        System.out.println(max);
    }


    static class Edge implements Comparable<Edge>{
        int end;
        int weight;

        public Edge(){

        }
        public Edge(int end,int weight){
            this.end = end;
            this.weight = weight;
        }

        @Override
        public int compareTo(Edge o) {
            return this.weight-o.weight;
        }
    }
}

上一篇:Python中if __name__ == ‘__main__‘的含义


下一篇:采用merge语句的非关联形式再次显神能