1003 Emergency

這鬼題目就不能用Java來作。各類超時。TMDjava

還有莫名其妙的答案錯誤。總之沒拿滿分-.-。233spa

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

煩的要命。blog

相關文章
相關標籤/搜索