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。加栈回溯。

烦的要命。

posted @ 2019-05-21 03:19  秃桔子  阅读(256)  评论(0编辑  收藏  举报

如果您有编程方面或者学术方面的需求请在微信公众号搜索

桔子科研


或者识别下方二维码,第一时间获取编程有趣的知识和最新科研学术成果。