zoukankan      html  css  js  c++  java
  • Java实现 蓝桥杯 算法提高 道路和航路

    问题描述
    农夫约翰正在针对一个新区域的牛奶配送合同进行研究。他打算分发牛奶到T个城镇(标号为1…T),这些城镇通过R条标号为(1…R)的道路和P条标号为(1…P)的航路相连。

    每一条公路i或者航路i表示成连接城镇Ai(1<=A_i<=T)和Bi(1<=Bi<=T)代价为Ci。每一条公路,Ci的范围为0<=Ci<=10,000;由于奇怪的运营策略,每一条航路的Ci可能为负的,也就是-10,000<=Ci<=10,000。

    每一条公路都是双向的,正向和反向的花费是一样的,都是非负的。

    每一条航路都根据输入的Ai和Bi进行从Ai->Bi的单向通行。实际上,如果现在有一条航路是从Ai到Bi的话,那么意味着肯定没有通行方案从Bi回到Ai。

    农夫约翰想把他那优良的牛奶从配送中心送到各个城镇,当然希望代价越小越好,你可以帮助他嘛?配送中心位于城镇S中(1<=S<=T)。

    输入格式
    输入的第一行包含四个用空格隔开的整数T,R,P,S。

    接下来R行,描述公路信息,每行包含三个整数,分别表示Ai,Bi和Ci。

    接下来P行,描述航路信息,每行包含三个整数,分别表示Ai,Bi和Ci。

    输出格式
    输出T行,分别表示从城镇S到每个城市的最小花费,如果到不了的话输出NO PATH。
    样例输入
    6 3 3 4
    1 2 5
    3 4 5
    5 6 10
    3 5 -100
    4 6 -100
    1 3 -10
    样例输出
    NO PATH
    NO PATH
    5
    0
    -95
    -100
    数据规模与约定
    对于20%的数据,T<=100,R<=500,P<=500;

    对于30%的数据,R<=1000,R<=10000,P<=3000;

    对于100%的数据,1<=T<=25000,1<=R<=50000,1<=P<=50000。

    2 解决方案
    本题主要考查最短路径,其中时间效率最好的算法为SPFA算法,但是下面的代码在蓝桥系统中评分为30或者35分,原因:运行超时,而同样的方法,用C来实现在蓝桥系统中评分为95或者100分,用C实现的代码请见文末参考资料1。

    如果有哪位同学Java版本代码测评分数超过50分的同学,还望借鉴一下代码,不甚感激~

    import java.util.ArrayList;
    import java.util.Scanner;
    
    public class Main {
        public static int T, R, P, S;
        public static ArrayList<edge>[] map;
        public static int[] distance;
        
        
        static class edge {
            public int a;  //边的起点
            public int b;  //边的终点
            public int v;  //边的权值
            
            public edge(int a, int b, int v) {
                this.a = a;
                this.b = b;
                this.v = v;
            }
        }
        
        @SuppressWarnings("unchecked")
        public void init() {
            map = new ArrayList[T + 1];
            distance = new int[T + 1];
            for(int i = 0;i <= T;i++) {
                map[i] = new ArrayList<edge>();
                distance[i] = Integer.MAX_VALUE;
            }
        }
        
        public void spfa() {
            ArrayList<Integer> town = new ArrayList<Integer>();
            distance[S] = 0;
            town.add(S);
            int[] count = new int[T + 1];
            boolean[] visited = new boolean[T + 1];
            count[S]++;
            visited[S] = true;
            while(town.size() != 0) {
                int start = town.get(0);
                town.remove(0);
                visited[start] = false;
                for(int i = 0;i < map[start].size();i++) {
                    edge to = map[start].get(i);
                    if(distance[to.b] > distance[start] + to.v) {
                        distance[to.b] = distance[start] + to.v;
                        if(!visited[to.b]) {
                            town.add(to.b);
                            visited[to.b] = true;
                            count[to.b]++;
                            if(count[to.b] > T)  //此时有负环出现
                                return;
                        }
                    }
                }
            }
        }
        
        public void getResult() {
            spfa();
            for(int i = 1;i <= T;i++) {
                if(distance[i] == Integer.MAX_VALUE)
                    System.out.println("NO PATH");
                else
                    System.out.println(distance[i]);
            }
        }
        
        
        public static void main(String[] args) {
            Main test = new Main();
            Scanner in = new Scanner(System.in);
            T = in.nextInt();
            R = in.nextInt();
            P = in.nextInt();
            S = in.nextInt();
            test.init();
            for(int i = 1;i <= R;i++) {
                int a = in.nextInt();
                int b = in.nextInt();
                int v = in.nextInt();
                map[a].add(new edge(a, b, v));
                map[b].add(new edge(b, a, v));
            }
            for(int i = 1;i <= P;i++) {
                int a = in.nextInt();
                int b = in.nextInt();
                int v = in.nextInt();
                map[a].add(new edge(a, b, v));
            }
            test.getResult();
        }
    }
    
  • 相关阅读:
    java8新特性→方法和构造函数引用:替代Lambda表达式
    java8新特性→Stream流:用于解决已有集合类库既有的弊端
    java8新特性→函数式接口
    java8新特新→Lambda表达式
    子查询
    Vue之监听数据变化watch、computed、methods
    Vue路由-使用命名视图实现经典布局
    Vue路由-使用children属性实现路由
    Vue之路由传参
    Vue路由之touter-link、router-direct的使用
  • 原文地址:https://www.cnblogs.com/a1439775520/p/12947870.html
Copyright © 2011-2022 走看看