zoukankan      html  css  js  c++  java
  • 1003 Emergency

    这鬼题目就不能用Java来做。各种超时。TMD

    还有莫名其妙的答案错误。总之没拿满分-.-。233

    import java.util.*;
    
    public class Main{
        static class Vertex {
            public int id; // 节点名字
            public int CityRescus;// 城市所含救援队个数
            public Edge next; // 节点的下一个弧
        }
    
        static class Edge {
            public int name; // 被指向的顶点
            public int weight; // 弧的权值
            public Edge next; // 被指向的下一段弧
    
        }
    
    //    static List<Vertex> list = new ArrayList<>();
        static Vertex[] list;
        static int N = 0;// 节点数
        static int M = 0;// 边数
        static int C1 = 0;// 起始点
        static int C2 = 0;// 终止点
        static Stack<Integer> stack = new Stack<>();
        static int roadNum = 0;
        static int minRoad = 0;
        static int mostMan = 0;
        static int len = 0;
        static int mans = 0;
    
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            N = sc.nextInt();
            M = sc.nextInt();
            C1 = sc.nextInt();
            C2 = sc.nextInt();
            list = new Vertex[N];
            for (int i = 0; i < N; i++) {
                int cityRescus = sc.nextInt();
                Vertex vertex = new Vertex();
                vertex.id = i;
                vertex.CityRescus = cityRescus;
                list[i] = vertex;
            }
    
            for (int i = 0; i < M; i++) {
                int x = sc.nextInt();
                int y = sc.nextInt();
                int z = sc.nextInt();
                Vertex X = list[x];
                Vertex Y = list[y];
                Edge edgeX = new Edge();
                edgeX.name = y;
                edgeX.weight = z;
    
                Edge edgeY = new Edge();
                edgeY.name = x;
                edgeY.weight = z;
    
                Edge Now = X.next;
                if (Now == null)
                    X.next = edgeX;
                else {
                    while (Now.next != null) {
                        Now = Now.next;
                    }
                    Now.next = edgeX;
                }
    
                Now = Y.next;
                if (Now == null)
                    Y.next = edgeY;
                else {
                    while (Now.next != null) {
                        Now = Now.next;
                    }
                    Now.next = edgeY;
                }
            }
            sc.close();
            DFS(C1, C2);
            System.out.print(roadNum + " " + mostMan);
        }
    
        public static void DFS(int C1, int C2) {
            //如果栈为空则压栈
            if (stack.size() == 0) {
                mans += list[C1].CityRescus;
                mostMan=mans;
                stack.push(C1);
            }
            //获得第一个边
            Edge e = list[C1].next;
            //当边不为空时遍历
            while (e != null) {
                //如果这条边已经遍历过则略过
                if (stack.contains(e.name)) {
                    e = e.next;
                    continue;
                }
                //压栈
                stack.push(e.name);
                //加路径长度
                len += e.weight;
                //加人
                mans += list[e.name].CityRescus;
                //如果当前就是目的地
                if (e.name == C2) {
                    //记录最短路径
                    if (len < minRoad || minRoad == 0) {
                        minRoad = len;
                        roadNum = 1;
                        mostMan = mans;
                    } else if (len == minRoad) {
                        roadNum++;
                        if (mans > mostMan)
                            mostMan = mans;
                    }
                    len -= e.weight;
                    mans -= list[e.name].CityRescus;
                    stack.pop();
                    break;
                }
                DFS(e.name, C2);
                len -= e.weight;
                mans -= list[e.name].CityRescus;
                stack.pop();
                e = e.next;
            }
            
        }
    }

    总体思路就是DFS。加栈回溯。

    烦的要命。

  • 相关阅读:
    oracle 调优3
    ifconfig找不到命令的帖子 精选
    执行计划中各字段各模块描述
    oracle统计信息
    oracle中 rownum与rowid的理
    触发器
    开园第一天
    Asp.net生成htm静态文件的两种途径
    避免刷新页面,自动跳回到页面顶部的办法
    ASP.NET二级域名站点共享Session状态
  • 原文地址:https://www.cnblogs.com/godoforange/p/10897659.html
Copyright © 2011-2022 走看看